how to show dialog box before downloading the dynamically generated pdf? - asp.net

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

Related

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

How to open an attachment straight from an InfoPath Form?

I would like to open an attachment directly from an InfoPath form, if possible.
I notice when viewing an InfoPath form in a web browser, I click the blue paperclip attachment icon, I am given three options—Attach, Download, and Remove (see screen shot). Yet when I open an InfoPath form in InfoPath's Preview environment, I get more options—Attach, Open, Save As, and Remove (see screen shot). Is there anyway to get this "Open" option available in the web browser?
Edit: Or perhaps there is a way to get the attachment's base64 encoded string programmatically? Does the File Attachment control have any kind of click event? I know I could add a button, use the button's click event and grab the base64 string that way, but I don't want to add any additionally UI elements.
I don't believe the attachement control has a click event, only the button.
Also, keep in mind that for the WEB version, even though you can grab the value of the attachment field (base64 encoded as you said) you won't be able to write it locally or spawn a process to "open" it unless the form has full trust.
The signing and/or install needed for full trust over the web is by far more complicated than the code to get the field value and save/open it. Don't forget to evaluate that aspect before spending too much time on a solution.

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.

In asp.net mvc2 how to open a new window and download a file

In my ASP.NET MVC2 app, I want to have a button in my view that collects some parameters off some form fields, then calls a controller action that returns a FileResult to stream a file (according to the parameters) to a new brower window.
I'm fine with using jquery AJAX to call the controller action, passing the paramters, but how do I get a new window to open for the streamed file?
EDIT: I know how to do stream the file back to the client browser - I'm just asking about how to write the View to collect some parameters to pass to the action.
For example, I have a fairly complex form, divided up into tabs using jquery.tabs(). On one of the tabs, the user can enter a dollar amount into a textbox, choose a template from the drop-down list then click a button to generate a voucher. So, I need to collect the values off the dollar amount box and drop-down selection (and I want to validate these, client-side) then call the controller action to generate a file to stream back to the browser.
So, my queestion is mainly a noob MVC question - what do I write in the view to specify a "mini form" that contains just those two fields, then validate them (I think I'm OK with that) then pass them to a controller action? (And I'm fine with using the link and specifying the target as _blank to open a new window.)
You usually don't call controller actions returning files using AJAX because you won't be able to do much with this file in the success callback (you cannot store the file to the client computer). So there are two possibilities: either the user is prompted with a dialog box to choose the location he wants to save the file or the contents of the file is opened inline with the default associated program (if there is any). It is the Content-Disposition header which is used to control this behavior:
Control-Disposition: attachment;filename=foo.pdf
will show a Save As dialog box to the user and it will stay on the same page. There won't be any redirect happening.
Control-Disposition: inline;filename=foo.pdf
will try to open the contents of the file inside the browser if there is an associated program and it will replace the current page.
If you want to open the contents of the file in a new browser window you could use target="_blank":
Download
Obviously this is only necessary if you are opening the file inline. For files that should be saved to the user computer you should simply provide the link and leave the user decide what to do with the file. No AJAX.
If I understand well, you must create a MemoryStream object
MemoryStream m = new MemoryStream();
Fill the object and then
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.ClearContent();
HttpContext.Current.Response.ClearHeaders();
HttpContext.Current.Response.ContentType = "application/ms-word";
HttpContext.Current.Response.AddHeader("Content-Disposition","attachment;filename=document.doc");
HttpContext.Current.Response.BinaryWrite(m.GetBuffer());
HttpContext.Current.Response.End();
Here the list of the mime-type allowed
http://msdn.microsoft.com/en-us/library/ms775147%28VS.85%29.aspx

Resources