0
votes

I want to use the WebBrowser control to display several files. But it is necessary to rename, remove the files after the WebBrowser control shows them. The problem is, that the files are locked until the WebBrowser is set to display another or no file.

Is there a way of forcing the WebBrowser to cache the file and release it immediatly after loading?


Now I created a memory stream from the file and pass it to the WebBrowser control.

using (var fileStream = new FileStream(source, FileMode.Open, FileAccess.Read, FileShare.Read))
{
  fileMemoryStream = new MemoryStream();
  fileStream.CopyTo(fileMemoryStream);
}

fileMemoryStream.Position = 0;
webBrowser.NavigateToStream(fileMemoryStream);

But now I don't see a pdf document but I see the pdf as ASCII code.

"%PDF-1.2 %ÈÇÐÄF 4 0 obj << /Type /Outlines /Count 0 >> endobj 5 0 obj......."

Is there any way to attach the program type from the file to the stream to show the PDF?

1
I know this is a little late to the party but what if you create a text file in memory and essentially construct the contents of an http response, including the headers (content-type: text/pdf or whatever the proper type is). Then embed the bytes from your pdf into the response and feed that whole thing to the web browser? Heck, you might be able to just use the HttpResponse class in .net to do all of this and serialize that to a memory stream. - Newclique

1 Answers

1
votes

Well the way WPF browser works it's might be not possible to display PDF from stream unfortunately, unless you do some hacking like running your own small http server inside application and stream pdfs using that server. But in your case, it's much easier to just copy pdf to temp folder and open from there:

File.Copy("path_to_pdf", Path.Combine(Path.GetTempPath(), Guid.NewGuid().ToString() + ".pdf");

And display it from that location. Of course, don't forget to clean up after you are done with it.