I have retrieved the pdf file from Database and linked it to a "a href" tag to open pdf file using link. Now I want to give the open/save dialog box before a pdf file opened . - in javascript(by using onclick event in "a" tag to call the javascript)
You have to set the content-disposition header using C# to get this behavior in a browser.
Read this for more info
How do I prompt a "Save As" dialog for an accepted mime type?
Downloading a File with a Save As Dialog in ASP.NET
Example
Response.Clear();
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileName);
Response.AddHeader("Content-Length", targetFile.Length.ToString);
Response.ContentType = "application/pdf";
Response.WriteFile(targetFile.FullName);
Related
On site (ASP.NET) i have table with items. Every item has link to specific pdf. This pdf i generate from binary data that are saved in database. So realy, i don't have path to pdf file.
When i click on link, i want to download file. And this functionallity work fine.
But i download file to default location that use browser (By default it's C:\Users\Admin\Documents)
What should i do to make browser show save dialog where user can chose destination path for pdf?
Code to generate pdf file :
private void ProcessFile(byte[] file, string fileName)
{
Response.Clear();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition",
string.Format("attachment;filename=\"{0}\"", fileName));
Response.BinaryWrite(file);
Response.Flush();
Response.End();
}
About my code : i'm going to rewrite a bit it according to this post
I am trying to open a file in the browser using BinaryWriter. This causes the a dialog window to open and prompt the user to save or open the file. That behavior is fine; however, if I select open, the aspx page is called again and after a long wait the file finally opens.
I set the ContentType
Response.BinaryWrite(binary);
Response.End();
Repsonse.Close();
This behavior only occurs with excel and word files.
Browser IE8
A substitute way of doing the same. You can check it out:
FileInfo fileInfo = new FileInfo(yourFilePath);
context.Response.Clear();
context.Response.ContentType = contentType; //Set the contentType
context.Response.AddHeader("content-disposition", "attachment; filename=" + Path.GetFilename(yourFilePath));
context.Response.AddHeader("Content-Length", fileInfo.Length.ToString());
context.Response.TransmitFile(yourFilePath);
Hope this will help
i'm trying to write code that download a file that located on the server.
but the save as dialog wont open in IE.
i tried response.redirect, i tried
Response.Clear();
Response.ContentType = "text/csv";
Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay);
Response.WriteFile(Server.MapPath(pathName + fileNameUnique));
Response.Flush();
Response.End();
every thing works on firefox and chrome , but not in internet explorer.
i know that there is a security option for this in security ---> custom level ---> Downloads ---> automatic prompting for file downloads , that is always in disable mode and i need to switch it to enable in order it to work, but i don't wont that my users deal with this.
how do i overcome this 'security problem' ?
is there any right way to deal with download files ?
what is the right code to do that ?
thank you,
gadym
var info = new FileInfo(path);
Response.Clear();
Response.AppendHeader("Content-Disposition", String.Concat("attachment; filename=", info.Name));
Response.AppendHeader("Content-Length", info.Length.ToString(System.Globalization.CultureInfo.InvariantCulture));
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.ContentType = "text/csv";
Response.WriteFile(info.FullName, true);
Response.End();
You need explicitly tell that it is not opened content,
add following headers:
Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Also it is strongly recommend to set explicit file length
Response.AppendHeader("Content-Length", responseContentLength.ToString());
abatishchev, thank you for helping me.
i found a soultion to my problem.
I created a dialog window (let's call it 'DownloadWindow') which holds an empty 'A HREF' tag.
(it's shows - 'click here To Download')
after i click a download (EXCEL/CSV icon) button on my default page (which creates my file
dynamicly),
i load the dialog window ('DownloadWindow') and then i'm repleacing the 'a href' link to the file url i created earlier so my users can download it from my server.
Now, Internet explorer did pop up the open/save/cancel dialog box.
it's a bit annoying but it's solved my porblem.
I had the same problem when my link for file download was made using:
<a href="DownloadFile.aspx">
<input type="image" src="~/virtual/path/to/image.png" runat="server" />
</a>
The problem was gone when i changed it to:
<a href="DownloadFile.aspx">
<img src="~/virtual/path/to/image.png" runat="server" />
</a>
I have ASP.NET page with an iframe on it for displaying some pdf reports on this page.
When user select the report type from dropdown, I add the needed for report data into the ASP.NET Session and change the attribute "src" of the iframe to .ashx module address which generates the .pdf report.
But if Adobe glug-in for viewing .pdf files in browser is not installed, browser proposes to save report file and name of the proposed file is "HandlerName.ashx".
But I want to make the browser proposed to save the file with the name "Report.pdf". Can I do this? Are there any workarounds?
Add the following header in your ashx-file:
context.Response.AddHeader("content-disposition", "attachment;filename=PUTYOURFILENAMEHERE.pdf");
context.Response.ContentType = "application/pdf";
Try inline content-disposition, however I have to check this:
Response.ContentType = "application/pdf";
Response.AppendHeader("content-disposition", "inline; filename=" + name );
Use
Response.Clear();
Response.ContentType = "application/octet-stream";
Response.AddHeader("Content-Disposition", "attachment; filename=Report.PDF");
Response.WriteFile(Server.MapPath("~/YourPath/Report.PDF"));
Response.End();
How can i show a Save as window in asp.net and C#...
This is similar to the one that opens for uploading but that window is the open file...
I am not looking to save only one file...like
Response.AddHeader("Content-Disposition", "attachment; filename=" + myfile.Name);
can i make it ung ajax... please help... samples or suggestions..
thanks
As far as I know you don't have any way of interacting with the file system using javascript from inside the browser. I would imagine that this would be a fairly large security hole if you could. The best I can suggest is to package up your files in a zip file then send that in a response with the content disposition set to attachment, then let the browser handle it for you.
In your page:
<iframe src="getFile.aspx?id=1"/>
<iframe src="getFile.aspx?id=2/>
Then inside getFile.aspx
Response.AddHeader("Content-Disposition", "attachment;filename=" + filename);
Response.BinaryWrite(fileContents);
(Note: never tried this myself...)