允许用户下载文件 - ASP.NET [英] Allowing users to download files - ASP.NET

查看:117
本文介绍了允许用户下载文件 - ASP.NET的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建一个音频文件的应用程序。我需要提供时cicked为用户保存文件,带来了保存对话框的链接。

I have an application that creates an audio file. I need to provide a link that brings up the save dialog when cicked for users to save the file.

我的下载按钮设置为链接按钮。从我的研究似乎是能够迫使造就了保存对话框,我需要添加以下code:

I have the download button setup as a link button. From my research it appears that to be able to force bringing up the save dialog, I need to add the following code:

Response.ContentType = "audio/wav";
Response.AppendHeader("Content-Disposition", "attachment; filename=blah.wav");
Response.WriteFile(@"blah.wav");
Response.End();

当我将它添加到链接按钮的事件处理程序,我得到的错误:

When I add this to the event handler of the link button, I get the error:

"Line: 4723
Error: Sys.WebForms.PageRequestManagerParserErrorException: The message received from the server could not be parsed. Common causes for this error are when the response is modified by calls to Response.Write(), response filters, HttpModules, or server trace is enabled.
Details: Error parsing near 'RIFF?WAVEfmt '."

所以,我决定尝试一下,通过创建一个中间页,便于下载。在下载LinkBut​​ton的事件处理程序,我添加了一个 Respose.Redirect 这中间页,在页面的Page_Load中,我加入了code段以上。如果按此方式设置,IE浏览器,阻止下载说:

So, I decided to try it out by creating an intermediate page that facilitates the download. On the Download linkbutton event handler, I added a Respose.Redirect to this intermediate page and on the Page_Load of the page, I added the code snippet above. When setup this way, IE, blocks the download by saying:

为帮助保护您的安全,Internet Explorer下载文件到您的计算机阻止他的网站。单击此处查看选项当我点击并选择下载文件,我没有得到保存对话框。

"To help protect your security, Internet Explorer blocked his site from downloading files to your computer. Click here for options" When I click and select "Download file", I do not get the Save dialog.

我的提问: 1.我可以消除中间页? 2.如果我不能我怎么没有IE浏览器提示用户进行操作?

My questions: 1. Can I eliminate the intermediate page? 2. If I can't how do I not have IE prompt the user for action?

推荐答案

也许你的响应包含的东西打你的code之前,你有没有尝试 Response.Clear()输出任何东西之前?

Maybe your Response contains something before hitting your code, did you try a Response.Clear() before outputting anything?

此外,不应该有任何的Response.Write 后,你的到Response.End()

Also, there shouldn't be any Response.Write after your Response.End().

这将解决的第一个错误,并将不再需要的中间网页。

This would fix the first error and would eliminate the need for an intermediate page.

我已经这样做过,所以我不看的限制。

I already done this before so I don't see a limitation.

试着找什么修改响应

感谢

这篇关于允许用户下载文件 - ASP.NET的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆