Bon*_*ton 2 asp.net-mvc download asp.net-mvc-2
我有
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;
}
}
Run Code Online (Sandbox Code Playgroud)
当然,ajax形式
<% var form = Ajax.BeginForm(...) %>
<input type="image" src="...gif" /> // this is my 1st attempt
<%= Ajax.ActionLink(...) %> // 2nd attempt
<% form.EndForm(); %>
Run Code Online (Sandbox Code Playgroud)
问:如何使用AsyncController制作一个漂亮的文件下载
问:如何使Ajax.ActionLink变得更好
您无法使用Ajax从服务器下载文件.这样做的原因是,即使您成功在success回调中向服务器发出异步请求,您也将获得从服务器发送的文件内容,并且您无法在客户端对此文件进行太多操作.请记住,javascript无法访问文件系统,因此您无法保存它.实现这一目标的方法是使用一个<form>指向Download操作的普通HTML .提交此表单后,将要求用户选择要保存文件的位置,然后继续下载.此外,您不需要AsyncController.
这是一个例子:
public class FileController : Controller
{
[HttpPost]
public ActionResult Download()
{
return File(
Encoding.UTF8.GetBytes("10k file size"),
"application/vnd.xls",
"test.xls"
);
}
}
Run Code Online (Sandbox Code Playgroud)
在你的视图中:
<% using (Html.BeginForm("download", "file", FormMethod.Post)) { %>
<input
type="image"
src="<%: Url.Content("~/content/images/download.png") %>"
value="download"
alt="download"
/>
<% } %>
Run Code Online (Sandbox Code Playgroud)
为了使下载按钮看起来不错,您可以创建一个很好的 download.png图像,用作表单提交按钮.
| 归档时间: |
|
| 查看次数: |
2863 次 |
| 最近记录: |