jQuery Asp.Net MVC 2 中的文件下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3597179/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
File download in Asp.Net MVC 2
提问by Anders
I want to enable file download in my MVC application, without simply using a hyperlink. I plan to use an image or the like and make it clickable by using jQuery. At the moment I have a simple just for testing.
我想在我的 MVC 应用程序中启用文件下载,而不是简单地使用超链接。我打算使用图像等,并通过使用 jQuery 使其可点击。目前我有一个简单的只是为了测试。
I found an explanation of doing the download through an action method, but unfortunately the example still had actionlinks.
我找到了通过操作方法进行下载的解释,但不幸的是,该示例仍然有操作链接。
Now, I can call the download action method just fine, but nothing happens. I guess I have to do something with the return value, but I don't know what or how.
现在,我可以很好地调用下载操作方法,但没有任何反应。我想我必须对返回值做一些事情,但我不知道做什么或如何做。
Here's the action method:
这是操作方法:
public ActionResult Download(string fileName)
{
string fullName = Path.Combine(GetBaseDir(), fileName);
if (!System.IO.File.Exists(fullName))
{
throw new ArgumentException("Invalid file name or file does not exist!");
}
return new BinaryContentResult
{
FileName = fileName,
ContentType = "application/octet-stream",
Content = System.IO.File.ReadAllBytes(fullName)
};
}
Here's the BinaryContentResult class:
这是 BinaryContentResult 类:
public class BinaryContentResult : ActionResult
{
public BinaryContentResult()
{ }
public string ContentType { get; set; }
public string FileName { get; set; }
public byte[] Content { get; set; }
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.ClearContent();
context.HttpContext.Response.ContentType = ContentType;
context.HttpContext.Response.AddHeader("content-disposition",
"attachment; filename=" + FileName);
context.HttpContext.Response.BinaryWrite(Content);
context.HttpContext.Response.End();
}
}
I call the action method via:
我通过以下方式调用操作方法:
<span id="downloadLink">Download</span>
which is made clickable via:
可通过以下方式点击:
$("#downloadLink").click(function () {
file = $(".jstree-clicked").attr("rel") + "\" + $('.selectedRow .file').html();
alert(file);
$.get('/Customers/Download/', { fileName: file }, function (data) {
//Do I need to do something here? Or where?
});
});
Note that the fileName parameter is received correctly by the action method and everything, it's just that nothing happens so I guess I need to handle the return value somehow?
请注意,操作方法和所有内容都正确接收了 fileName 参数,只是没有任何反应,所以我想我需要以某种方式处理返回值?
回答by Ian Mercer
You don't want to download the file using AJAX, you want the browser to download it. $.get() will fetch it but there's no way to save locally from Javascript, for security reasons the browser needs to be involved. Simply redirect to the download location and the browser will handle it for you.
您不想使用 AJAX 下载文件,而是希望浏览器下载它。$.get() 将获取它,但无法从 Javascript 本地保存,出于安全原因,浏览器需要参与。只需重定向到下载位置,浏览器就会为您处理。