I have news articles that get written in MS Word and I want to be able to insert these into the db to be displayed on my website. What would be the best way of doing this and how would I keep line breaks ect? Thanks
I assume the reason you are posting from word is to be able to use the formating that you added while in word. There are two ways I can think of to do this.
1 make the final document an image and save that to the database. You could also save it as a pdf. An image can easily be saved to a database. These may have scaling issues depending on the screen size and resolution. As an example, online magazines are
having to reformat their pages to match the new retena display.
2 Include a Rich Text frame in your web site. This also allows some significant security risks and there are some compatability issues that you need to resolve. This frame allows some of the rich text but it is likely moderated and scanned.
Thanks Craig, I think I'll check out the rtf frames that you mention. I don't think I want to start storing images of text into the database if I can do it another way. Thanks for the tip
Yes, that is one of the problems with merging Javascript and Razor. Any escaping that is done in the Javascript is removed by the request object in the razor section. You will have to use the request.unvalidated().form call to get any of the html that
you want to send to razor code. Keep in mind that this data could contain dangerous code such as SQL Injection so you should also escape it immediatly with server.htmlencode if you plan to save it to a database.
This is the reason for your getting that warning. The data in Rich Text can contain harmful scripts and you need to keep that in mind when using it.
tonyjoanes
Member
24 Points
37 Posts
paste news article from word and save in db
Apr 13, 2012 07:18 AM|LINK
dow7
Member
732 Points
449 Posts
Re: paste news article from word and save in db
Apr 13, 2012 11:09 AM|LINK
you can read more here : http://www.asp.net/web-pages/tutorials/data/5-working-with-data and could use this code to enter news articles into the db :
@{ Layout = ""; Page.Title = ""; var db = Database.Open("dbname"); var Title = Request["Title"]; var Content = Request["Content"]; var Date = Request["Date"]; if (IsPost) { var insertQuery = "INSERT INTO newsarticles (Title, Content, Date) " + "VALUES (@0, @1, @2)"; db.Execute(insertQuery, Title, Content, Date); } } <h2>Add news Articles</h2> <form method="post" action=""> <div> <p>Date: <input name="Date" type="text" size="30" value="@Date" /> </p> <p>Title: <textarea name="Title" rows="3" cols="74">@Title</textarea> </p> <p> Content: <textarea name="Content" rows="14" cols="74">@Content</textarea> </p> <input type="submit" value="Add" /> </div> </form>And use this new page to display :
@{ Layout = ""; Page.Title = ""; var pageSize = 10; var totalPages = 0; var count = 0; var page = UrlData[0].IsInt() ? UrlData[0].AsInt() : 1; var offset = (page -1) * pageSize; var db = Database.Open("dbname"); var sql = @"SELECT Count(*) FROM newsarticles "; count = (int)db.QueryValue(sql); totalPages = count/pageSize; if(count % pageSize > 0){ totalPages += 1; } sql = "SELECT Title, Content, Date FROM newsarticles" + "ORDER BY Id DESC OFFSET @0 ROWS FETCH NEXT @1 ROWS ONLY;"; var result = db.Query(sql, offset, pageSize); } <h2>News Articles</h2> @foreach(var row in result){ <p>@row.Date<br/> <strong>@row.Title</strong><br/> @Html.Raw(row.Content.Replace("\n", "<br />")) </p> } <p> @{ for (var i = 1; i < totalPages + 1; i++){ <a href="/informations/@i">@i</a> } } <span class="alignRight"> Page @page / @totalPages</span> </p>You can keep line breaks by using
@Html.Raw(row.Content.Replace("\n", "<br />"))craig_inb
Member
76 Points
31 Posts
Re: paste news article from word and save in db
Apr 16, 2012 11:34 AM|LINK
I assume the reason you are posting from word is to be able to use the formating that you added while in word. There are two ways I can think of to do this.
1 make the final document an image and save that to the database. You could also save it as a pdf. An image can easily be saved to a database. These may have scaling issues depending on the screen size and resolution. As an example, online magazines are having to reformat their pages to match the new retena display.
2 Include a Rich Text frame in your web site. This also allows some significant security risks and there are some compatability issues that you need to resolve. This frame allows some of the rich text but it is likely moderated and scanned.
tonyjoanes
Member
24 Points
37 Posts
Re: paste news article from word and save in db
Apr 16, 2012 01:00 PM|LINK
tonyjoanes
Member
24 Points
37 Posts
Re: paste news article from word and save in db
Apr 16, 2012 01:02 PM|LINK
craig_inb
Member
76 Points
31 Posts
Re: paste news article from word and save in db
Apr 16, 2012 01:45 PM|LINK
the one I used was tinymce although there others. Most are javascript plugins that are implimented in a textarea.
I'll be happy to answer any questions you have on this. I may save you several hours and probably help me understand it better too.
tonyjoanes
Member
24 Points
37 Posts
Re: paste news article from word and save in db
Apr 16, 2012 03:10 PM|LINK
tonyjoanes
Member
24 Points
37 Posts
Re: paste news article from word and save in db
Apr 17, 2012 09:15 PM|LINK
TinyMCE seems really very good although I'm having one problem. When posting back my form data I get the following message:
A potentially dangerous Request.Form value was detected from the client (article="<p><strong>Rich Text...").
I've tried @Html.Encode on the Request[""] object but that doesn't seem to help me, have you seen this before?
GmGregori
Contributor
5470 Points
737 Posts
Re: paste news article from word and save in db
Apr 17, 2012 09:31 PM|LINK
Maybe this link could help you: TinyMCE and “A potentially dangerous Request.Form value was detected”
craig_inb
Member
76 Points
31 Posts
Re: paste news article from word and save in db
Apr 19, 2012 12:47 PM|LINK
Yes, that is one of the problems with merging Javascript and Razor. Any escaping that is done in the Javascript is removed by the request object in the razor section. You will have to use the request.unvalidated().form call to get any of the html that you want to send to razor code. Keep in mind that this data could contain dangerous code such as SQL Injection so you should also escape it immediatly with server.htmlencode if you plan to save it to a database.
This is the reason for your getting that warning. The data in Rich Text can contain harmful scripts and you need to keep that in mind when using it.