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.A开发者_高级运维ddHeader("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>
精彩评论