如何在 AsyncControl 中 MVC 下载文件?
我有
public class FileController : AsyncController
{
public ActionResult Download(FormCollection form)
{
FileContentResult file = new FileContentResult(Encoding.UTF8.GetBytes("10k file size"),"application/vnd.xls");
file.FileDownloadName = "test.xls";
return file;
}
}
当然,
<% var form = Ajax.BeginForm(...) %>
<input type="image" src="...gif" /> // this is my 1st attempt
<%= Ajax.ActionLink(...) %> // 2nd attempt
<% form.EndForm(); %>
- ajax形式,我尝试第一种方法(输入类型=图像)。它达到正确的行动。但客户端没有文件下载。
- 然后我尝试使用我真的很讨厌的 Ajax.ActionLink 。我想要漂亮的图像按钮,而不是链接文本。再次,它达到了正确的操作并且没有文件下载。但如果我在另一个窗口中打开链接,就会有文件下载!
问 如何使用 AsyncController 下载一个漂亮的文件
问 如何使 Ajax.ActionLink 看起来不错
I have
public class FileController : AsyncController
{
public ActionResult Download(FormCollection form)
{
FileContentResult file = new FileContentResult(Encoding.UTF8.GetBytes("10k file size"),"application/vnd.xls");
file.FileDownloadName = "test.xls";
return file;
}
}
and ofcourse, ajax form
<% var form = Ajax.BeginForm(...) %>
<input type="image" src="...gif" /> // this is my 1st attempt
<%= Ajax.ActionLink(...) %> // 2nd attempt
<% form.EndForm(); %>
- i try first method(input type=image). it reach correct Action. but no file download in client side.
- Then i try to use Ajax.ActionLink which i really hate. i want nice image button, not link text. Again, it reach correct Action and no file download. But if i open link in another window, there's file download !!
Q. How to make a nice file downlaod with AsyncController
Q. How to make Ajax.ActionLink lok nice
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法使用 Ajax 从服务器下载文件。原因是,即使您在
success
回调中成功向服务器发出异步请求,您也将获得从服务器发送的文件内容,并且您无法在服务器上对该文件执行太多操作客户端。请记住,javascript 无法访问文件系统,因此您将无法保存它。实现此目的的方法是使用一个普通的 HTML这是一个示例:
在您的视图中:
为了使下载按钮看起来漂亮,您可以创建一个漂亮
download.png
图像它将用作表单提交按钮。You cannot use Ajax to download files from the server. The reason for this is that even if you succeed to make the async request to the server in the
success
callback you will get the file contents as sent from the server and you cannot do much with this file on the client side. Remember that javascript cannot access the file system so you won't be able to save it. The way to achieve this is to have a normal HTML<form>
which will point to theDownload
action. When this form is submitted the user will be asked to choose where he wants to save the file and the download will proceed. Also you don't need an AsyncController for this.Here's an example:
and inside your view:
And in order to make the download button look nice, you could create a nice
download.png
image which will be used as form submit button.我使用这个:
禁用
以启用
具有 3 提交 1 刷新 2 下载文件的表单
所有帖子都发布到相同的 ActionResult,但其中 2 个具有下载为 csv 或 pdf 的参数
,下载时只需禁用 ajax 提交。
I use this:
to disable
to enable
for a form with 3 submit 1 to refresh 2 to download file
All post to the same ActionResult but 2 of them have param to download as csv or as pdf
when download just disable ajax submission.
我找到办法了!!
只需创建具有常规形式的 iframe 并使用 jquery 触发即可。
:D
ps 归功于 Firebug + Google Doc。
i found a way!!
just create iframe that has regular form and use jquery to trigger to.
:D
ps credit to Firebug + Google Doc.