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
ExportToCSVFile(DtExcel);
Response.ContentType = "Application/x-msexcel";
Response.AddHeader("content-disposition", "attachment;filename=" + GetCustomerUserID() + "_UserList.csv");
Response.Write(ExportToCSVFile(DtExcel));
// Context.ApplicationInstance.CompleteRequest();
Response.Redirect("Redirect_Url_ExportToExcel.aspx", false);
// Context.ApplicationInstance.CompleteRequest();
// Response.End();
hi
I am exporting and saving the excel file. After saving the file the page is still doing some processing. To abort that processing i need to redirect the page. If I' redirect the page the SaveFileDilaog doesn't appear. If it appears at that time the page does not redirect. Please give some solution.
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 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);