ASP.NET 中 IE 7,8 中的“另存为”对话框出现问题

发布于 2024-09-12 00:57:39 字数 581 浏览 12 评论 0原文

我正在尝试编写代码来下载位于服务器上的文件。 但另存为对话框不会在 IE 中打开。

我尝试了response.redirect,我尝试了

 Response.Clear();
 Response.ContentType = "text/csv"; 
 Response.AddHeader("Content-Disposition", "attachment; filename=" + fileNameDisplay); 
 Response.WriteFile(Server.MapPath(pathName + fileNameUnique)); 
 Response.Flush(); 
 Response.End(); 

在 Firefox 和 chrome 上一切正常,但在 Internet Explorer 上不行。 我知道安全中有一个安全选项--->自定义级别--->下载--->自动提示文件下载,始终处于禁用模式,我需要将其切换为启用才能正常工作,但我不希望我的用户处理此问题。 我该如何克服这个“安全问题”?

有没有正确的方法来处理下载文件? 执行此操作的正确代码是什么?

谢谢你, 加迪姆

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

北城半夏 2024-09-19 00:57:39
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();
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();
软糖 2024-09-19 00:57:39

你需要明确告诉它不是打开的内容,
添加以下标头:

Response.AppendHeader("Content-Disposition", "attachment; filename=" + fname);
Response.AppendHeader("Content-Transfer-Encoding", "binary");

还强烈建议设置显式文件长度

Response.AppendHeader("Content-Length", responseContentLength.ToString());

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());
月亮是我掰弯的 2024-09-19 00:57:39

阿巴蒂什切夫,谢谢你帮助我。
我找到了解决问题的办法。

我创建了一个对话框窗口(我们称之为“DownloadWindow”),其中包含一个空的“A HREF”标签。
(它显示 - '单击此处下载')

在我单击默认页面上的下载(EXCEL/CSV 图标)按钮(这将创建我的文件
动态),
我加载对话框窗口(“DownloadWindow”),然后将“a href”链接替换为我之前创建的文件 URL,以便我的用户可以从我的服务器下载它。

现在,Internet Explorer确实弹出了打开/保存/取消对话框。

这有点烦人,但它解决了我的问题。

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.

忘年祭陌 2024-09-19 00:57:39

当我使用以下方式创建文件下载链接时,我遇到了同样的问题:

<a href="DownloadFile.aspx">
    <input type="image" src="~/virtual/path/to/image.png" runat="server" />
</a>

当我将其更改为:时问题就消失了:

<a href="DownloadFile.aspx">
    <img src="~/virtual/path/to/image.png" runat="server" />
</a>

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>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文