publicvoid LoadToLabel()
{
using (StreamReader rdr =newStreamReader(FileUpload1.PostedFile.FileName))
{
string uploadedHTML = rdr.ReadToEnd();
textLabel.Text = uploadedHTML.ToString();
}
}
So far I have that, but the problem is that if the HTML file has an image displayed in it, the image doesn't show up because it's just copying the text. How would I be able to transfer the entire HTML page to a control? Like a panel or something like that, is it possible? And have the images come with it?
You should use LiteralControl for this purpose.
So if I switch it to a literal would it display the images as well?
I just switched it to a literal control and it still ignores the images. What must I do?
Yup. The LiteralControl will display the HTML exactly as you create it. See it as a place holder for output you create from code behind. Good luck!
OK, let me get this straight. You upload a HTML document containing <img> tags, which you then want to print out in the page, right?
If you do a view source in the resulting page, how does it look?
If the image urls in the uploaded page are relative then that is maybe what's causing the problem. Please give an example of how this tag looks like.
Also, if you upload a complete HTML document (with head/body) then you will render a page that is not valid (containing duplicate elements).
What does image urls being relative mean? Like pointing to a specific spot and not "~/Images/image.gif" etc.?
I did a view source on the page that uploads and displays the HTML document and yes you're right, there are now duplicate elements. It uploads everything within the uploaded documents <HTML></HTML> tags. How do I go about just grabbing what's in the body, and not the body tags themselves as that would be a duplicate element as well?
A relative path is one that does not begin with a / or http://something, example:
<img src="http://pics.10026.com/?src=images/ticker.gif">
A relative path is always calculated from the place where it is accessed.
If your (uploaded) page contains relative image paths, then to display these images correctly, you need to either rebuild the path (by parsing), or copy the contents of the path into the same folder as your upload page (not a viable solution).
Or you could make sure the user uploads all the images as well.
In either way, you have some parsing to do in order to get the uploaded file to display correctly. Also you must parse the file to get the actual contents of the file, and get past <html><body> and so on.
One option you have is to show the uploaded file in an iframe which would allow you to show the document as-is (although images would still require parsing).
How would I go about parsing the uploaded file so it displays properly with the images included within the literal control?
Strange... I could almost see that question coming Well, parsing text is not trivial, and I am not able to give you the code as it would take some time to develop and test. But here's how I would do: Define a regexp, that finds <img tags, and then replaces the src value with your "path prefixed" value.
Problem is that these tags can come in so many flavours, it's almost impossible to guarantee that the parser matches them correctly. Parsing HTML is kind of re-inventing the wheel again (the wheel being the browser ;-)
Uhm, I'm sorry, can't help you any further with this. But if you google a little on Regular expressions and spend some time trialing and erroring then you might reach your goal. Good luck!
Thank you so much for your help!
0 comments:
Post a Comment