Problem displaying PDF in IE8 via Silverlight application - asp.net

I have a Silverlight 4 application from which I'm attempting to display a PDF. My approach has been to upon a button click in the Silverlight application, use HtmlPage.Window.Navigate to open a new browser window. The URL for that this new browser window navigates to is an ASP.Net web forms page that makes a call to SQL Reporting Services via the SSRS SOAP API. This call returns a byte array which the web form then streams back to the browser with the following code:
byte[] report = SSRSRenderReport(reportPath, primaryId);
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("cache-control", "must-revalidate");
Response.AddHeader("content-length", report.Length.ToString());
Response.Buffer = true;
Response.ContentType = "application/pdf";
this.Response.AddHeader("Content-Disposition", "inline; filename=whatever.pdf");
Response.BinaryWrite(report);
Response.Flush();
Response.End();
This all works quite well when running the application from IE9 and Firefox. However when running the application from IE8, the new browser window is displayed after the button click but then closes immediately without ever displaying the PDF or prompting to open/save the PDF.
If I take Silverlight out of the picture and just browse directly to the URL that renders the report, it works fine, the PDF is displayed in the browser. I've seen a few posts that describe this issue when HTTPS is being used, however I'm only using HTTP currently.
Any suggestions on how to get around this issue would be much appreciated.

Try this, http://www.divelements.com/silverlight/tools.aspx I used this to embed a flash site within a silverlight webpage, so as long as you are sure of the size of the pdf viewer you should be ok.

We discovered that the source of this problem was that we were attempting to display our popup window from a viewmodel rather than a view event handler. Eventually we opted to introduce a new view which contains a hyperlink pointing to the URL of the PDF. This works and was for us, an acceptable solution.

Related

Let the user download a generated file in ASP.Net

I'm creating an ASP.Net web application and I want to add an export feature. That feature will generate a string (xml, to be specific) when a button is clicked.
I want to enable the user to download this string as a file.
Do I need to create a file on the server's hard drive and open a link to that file or is there a way to download the string directly (so ASP.net takes care of creating and removing the file)?
I am currently tryig to do the following:
Response.Clear();
Response.Buffer = true;
Response.ContentEncoding = Encoding.GetEncoding(1252);
Response.ContentType = "application/vnd.ms-excel";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.txls");
//Multiple Response.Write(string) calls to add content to the file
Response.End();
Using firebug, I found out the the user receives the headers, but the file doesn't open.
The Internet Explorer is even able to show me the response text and has a button to save the file (both options are only avilable in the developer tools) and the file is correct, but nontheless, the file still doesn't open a window (nor a in-browser-preview).
When I use the Internet Explorer (which seems to have better Visual Studio debugging integration), a JavaScript error appears:
The EnableScriptGlobalization property cannot be changed during async postbacks or after the Init event.
Calls to Response.Write() seem to be a common cause of this issue. Of course, I call exactly that method multiple times.
I figured out that the JavaScript on the webpage (the script seems to be auto-generated) tries to parse the response, but of course it isn't formatted in the asp.NET internal communication format, but in my own XML format.
How can I tell the page to treat my page as a downloadable file?
string someXML=GetXMLContent();//I assume you have an implementation of this
Response.Clear();
Response.ContentType = "text/xml";
Response.AddHeader("Content-Disposition", "attachment; filename=filename.xml");
Response.Write(someXML);
Response.End();
Couple of things to note. It's probably better to build up your XML string early on, then write it to the response in one shot. It makes for cleaner code, because you can separate your concerns this way.
Also, I switched the content type to "text/xml", because this is not an Excel file. Also, your file extension was incorrect. XML by convention ends in .xml. I removed the content encoding because it's not necessary most of the time. If you're dealing with weird characters feel free to add it back. Also removed the buffer in the name of removing unnecessary things.
Try this, then try opening the resulting file in your text editor and verifying the content.
The problem was that the button responsible for triggering the Excel export was not a PostBackTrigger. As soon as I had changed it, everything worked like a charm.
Nontheless, I don't know hy this fixed everything. What does it change when I set the button as a PostBackTrigger

ASP.net serving PDF files in browser - issue with browser history

I'm using the following code to serve a PDF file to the browser:
HttpContext.Current.Response.ContentType = "Application/pdf";
string filename = "somefilename.pdf";
HttpContext.Current.Response.AddHeader("Content-Disposition",
"inline; filename=thefile.pdf");
HttpContext.Current.Response.WriteFile(filename);
HttpContext.Current.Response.End();
When this code fires, the URL doesn't change - it simply serves the PDF file.
In non-IE browsers, when I hit back, it goes back to the page that fired the action. However, in IE, I go back to the PREVIOUS page (i.e. the login page, not the page that serves the PDF files.
Just wondering what the best way to handle this so that IE users can click their browser's "back" button and getting a predictable response.
Hi i think you have to use a button to redirect to other webform and in load page copy your code to charge the information that u want to show in pdf and obviously too the code to show this like pdf
Try the history in the Scriptmanager control.
ASP.NET history

Winform browser control "attachment" button

We have a winform app that has a browser control on it. Previously these files (always very small 10kb etc.) were stored at a unc location. We would generate some html and load the html into the browser. If we wanted to make one of these small files available we would include in the HTML an anchor tag () WHen the html was displayed in the browser control so would be the link. The user could click on the link and the file save as dialog would appear.
We are now storing these files in the db as varbinary and thus there is no longer a physical location for the anchor tag to point to. I have several thoughts but would like the members of SO who are way smarter than me to chime in.
Option 1 in my mind would be to have an image button, anchor tag, something in the html to click on. I would handle the "onclick" either in javascript or as a postback. This seems doable for my level of knowledge EXCEPT I do not know how to get the byte[] to translate into the save as dialog for the user....do I render it to disk first?
The other idea I had was to have a button that is NOT in the browser control. This button would be hidden / visible if the biz rules said to show a file. Clicking on the button would then generate the byte[] which is easily turned into a file and the save as shown dialog shown in the winform app.
So any thought or all together different suggestions welcome
TIA
JB
I understand that you are in control of the ASP.NET web page shown in the windows forms web browser control so you can edit that page and build it the way you want.
if that is true, behavior in hosted web browser or in normal IE session is the same and I would suggest to create a bunch of hyper links or buttons in the asp.net web form page each one which a specific ID, like the ID of the file to download. then you can create an handler or a button_click event handler where you get the byte[] of the file by the clicked button/link associated file Id, or from query string if you initiated an handler call, and then you start streaming down to the browser the file content, the browser will do all what is required for you.
for example, just as a starting point, a bit of code taken from here: http://social.msdn.microsoft.com/Forums/en-US/silverlightnet/thread/d6a5087f-43b1-4782-95f1-d1376130d2c8
shows you a possible way to do this from a page load, the trick is that the call to GetDocument gets the proper file content for you (in this case from the query string, imagine like if we are inside an handler processing method) and returns a class DocumentInfo which contains the bytes. you do nor need this DocumentInfo, you can just have a method which returns byte[] by File Id for example...
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
string queryString = this.Request.QueryString.ToString();
if (string.IsNullOrEmpty(queryString)) return;
DocumentInfo documentInfo = GetDocument(queryString);
if (!documentInfo.HasValue) return;
Response.ClearHeaders();
Response.ClearContent();
Response.AppendHeader("Content-Length", documentInfo.Value.Content.Length.ToString());
Response.ContentType = "application/octet-stream";
Response.AppendHeader("Content-Disposition", "attachment; filename=Test.doc");
Response.BinaryWrite(documentInfo.Value.Content);
Response.End();
}
}

how to show dialog box before downloading the dynamically generated pdf?

I am using ASP.NET, C# and iTextSharp for creating a PDF.
Then I am using this code for transmitting the file.
Response.TransmitFile(filename);
So I want to display a dialog box which will request the user whether to open/save/cancel when they click on the generate button.
Thanks.
You can write it to the response directly. Browser will show the save as/open depending on the type sent.
Response.ContentType = "application/pdf";
Response.WriteFile(#"C:\Downloads\Test.pdf");
Response.Flush();
Responce.AddHeader("content-disposition","attachement;filename=name.pdf");
Response.TransmitFile(filename);
Here content-disposition is the one which is used for displaying the dialog box.
As I remember, is the navigator who decides make the download for load it inside navigator.
Normally the unique way to download it is offer a link and guide the user to right click and to choose Save As

PDF form submitted to ASP.NET page creates a return HTML file

I have a PDF form that has a 'Submit' button that submits its information to a ASP.NET page. The information is taken and processed into a PDF on the server for storage and later use.
This process works fine, however after the form is done submitting the Adobe viewer receives back a html 'fragment' i guess. I need to know how to format that, stop it, or otherwise handle it. I suspect the fragment is a success message but the Adobe reader doesn't seem to handle it
The HTML is
<!-- saved from url=(0014)about:internet -->
I don't have a definitive answer on how to get Acrobat to display your HTML, or if it even can, but my guess is that it won't display HTML back. I do remember having issues with this years ago, and when I had an HTTP Handler to take the form data and process it, but my final step was to redirect them code to different PDFs based on their status.
I put together different response PDF for a few different scenarios as I wasn't showing them back the document that was actually created and when the processing was done just did a context.Response.Redirect( "STATUS.pdf", false ); which Acrobat handled fine.
You could alternately try changing to ContentType to text/plain and seeing if Acrobat will display any output that way.
context.Response.ContentType = "text/plain";
context.Response.Write("Message here.");

Resources