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

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

Related

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.");

Problem displaying PDF in IE8 via Silverlight application

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.

After ASP .Net Dynamically Rendered PDF, Cannot Change Page

I have a page where the user enters some information. At the bottom of the page there is a checkbox that is disabled, that says they have read the PDF to be displayed. They click on a link which dynamically renders a PDF and is downloaded by the user. After downloading the PDF a Response.End() is issued, which means nothing else can be done on the page. I want to be able to enable the checkbox after the PDF is sent.
I have tried saving the data to the Session, opening a new window, and using the Session data to send the PDF from that new window. However, IE popup-blocker blocks it.
Any ideas?
Is there anyway to use jQuery/AJAX for this? If you did you could send the request to download the PDF via the AJAX request, and then re-enable the checkbox with javascript afterwards.

Unblock (jQuery BlockUI) after file sent to browser through response stream

I have an <asp:button /> that generates a PDF report. I want to use jQuery BlockUI to display a message that says: Generating Report..
Using: Response.BinaryWrite I am then sending the file to the user so it appears as a file download in the browser.
I cannot get $.unblockUI(); to fire. As soon as the file download begins or has completed or is cancelled I want it to dissappear. Currently it never does.. It's as though the page hasn't been re-loaded, I've hit the server but it comes back with the same page.
Things I have tried:
Setting a variable in JS to true so on document.ready() it will call a function if set to true. This doesn't work because even though I change the variable in code it doesn't change the HTML that is sent to the client.
This sort of thing: Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(function() { $.unblockUI; }); //register with Microsoft way
$(document).ajaxStop($.unblockUI); //double the insurance and register with jquery way
never gets called..
Can this be achieved with an updatepanel?
Any thoughts?
Also in case it helps:
Response.AddHeader("Content-Disposition", "attachment;filename=""" & ShortFilename & """")
Response.AddHeader("Content-Length", byteArray.Length)
Response.BinaryWrite(byteArray)
Response.Flush()
Response.End()
I can see why this doesn't work sort of, the page is not refreshing in anyway there's just a response stream being sent to the browser, but surely there's an event I can latch on to?
An idea could be to create a child window that does the PDF loading and let the parent figure out when the child window has closed or something.
Is it possible for parent window to notice if child window has been closed?
The solution is to first block the UI as normal. Then make an AJAX request to your report generator, when the PDF is generated store it locally for a short time, or have it in a temporary folder that is cleared out when the user logs out or their login times out. Have the report generator return a success message and a URL.
Have the client ajax request handle the success message, remove BlockUI, then call the URL using:
window.location="http://yourURL/temp/report.pdf
The browser will start to download the file and you are done!
https://stackoverflow.com/a/7660817/181197

opening a html page on the client from server side

On the server I receive xml from a webservice, I use xslt transformation on this xml to create a htm page. Now I need to show this htm page to the user by opening it in a new browser window. How do I achieve such functionality? My website is written in ASP.NET.
I have tried using
Response.Write("");
Response.Write("window.open('" + Server.MapPath("~/App_Data/HTMLPage.htm") + "','_blank')");
Response.Write("");
But this throws me an access denied error.
Thanks in advance.
Chandrasekhar
As I understood it, you want this new page to open in a new browser window, correct?
If so, you're going about it the wrong way. Response.Redirect will only redirect the current page, not instantiate a new browser window.
What you need to do is inject a JavaScript command into the page that opens a new browser page. That command is window.open. Here's a quick way to do it:
ClientScript.RegisterStartupScript(this.GetType(), "newpage", "window.open('" + address +"');", true);
This code will insert the JavaScript command to execute when the page reloads after submission. Note that address is the string variable that contains the address of the page you want to open.
Another very important note: most browsers will consider this a pop-up window and may very well block it.

Resources