C# 如何用AJAX和MVC实现文件下载
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14087743/
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
How to implement file download with AJAX and MVC
提问by Arcadian
I would like to provide a file download operation by using the jQuery AJAX call with some params under MVC
我想通过在MVC下使用带有一些参数的jQuery AJAX调用来提供文件下载操作
Example
例子
(javascript)
function DoDownload(startDate) {
$.ajax({
url:"controller/GetFile/",
data: {startDate:startDate}
...
});
}
C# Controller Code
public void GetFile(string startDate) {
var results = doQueryWith(startDate);
// Create file based on results
....
// How do I tell the server to make this a file download??
}
I typically would just make my file download a link such as:
我通常只会让我的文件下载一个链接,例如:
<a h r e f="mycontroller/getfile/1"/>Download</a>
but in the case above the date will be dynamic.
但在上述情况下,日期将是动态的。
If I don't use ajax, what would be a preferred way to pass in the params to the MVC controller using javascript?
如果我不使用 ajax,使用 javascript 将参数传递给 MVC 控制器的首选方法是什么?
Example:
例子:
window.location = "mycontroller/GetFile/" + $("#fromDate").val();
assuming the date is 12-25-2012
假设日期是 12-25-2012
Would this produce
这会产生
mycontroller/GetFile/12/25/2012
would MVC treat this as three params?
MVC 会将此视为三个参数吗?
采纳答案by Arcadian
What I ended up doing is calling my controller from my javascript like:
我最终做的是从我的 javascript 调用我的控制器,例如:
var url = "/mycontroller/GetFile?startDate=" + $("#mydate").val() + etc...
window.location = url;
mycontroller.cs
mycontroller.cs
public void GetFile(DateTime startDate)
{
}
My original concern was with the date parameters. I didnt want to have to parse it.
我最初关心的是日期参数。我不想解析它。
回答by M.Ob
Your controller action method should return a FileResult instead of void. And there is no need to do this via AJAX - in fact, you don't want to do this with AJAX. You'll need the browser involved so it knows to provide a download dialog for the user.
您的控制器操作方法应返回 FileResult 而不是 void。并且不需要通过 AJAX 执行此操作 - 事实上,您不想使用 AJAX 执行此操作。您将需要所涉及的浏览器,以便它知道为用户提供下载对话框。
See these links:
请参阅这些链接:
Handling an ASP.NET MVC FileResult returned in an (jQuery) Ajax call
处理在 (jQuery) Ajax 调用中返回的 ASP.NET MVC FileResult
File download in Asp.Net MVC 2
I hope this helps.
我希望这有帮助。
回答by Shyju
You can use the File
method of controller class to return a file back to the browser.
您可以使用File
控制器类的方法将文件返回给浏览器。
The below sample returns a pdf file.
下面的示例返回一个 pdf 文件。
public ActionResult GetFile(int id)
{
var fileInfo=repositary.GetFileDedetails(id);
var byteArrayOFFile=fileInfo.FileContentAsByteArray();
return File(byteArrayOFFile,"application/pdf","yourFriendlyName.pdf");
}
Assuming repositary.GetFileDedetails
method returns the details of the file from the id.
假设repositary.GetFileDedetails
方法从 id 返回文件的详细信息。
You may also return the file from a physical location(a path) or a stream. Check all the overloadsof the File
method and use appropriate one.
您还可以从物理位置(路径)或流返回文件。检查所有重载的的File
方法,并使用合适的一个。
This has nothing to do with ajax. this is normal GET
request over a browser.
这与ajax无关。这是GET
通过浏览器的正常请求。
回答by Romias
Using the ActionLink helper, you can pass multiple params to your controller:
使用 ActionLink 助手,您可以将多个参数传递给您的控制器:
HtmlHelper.ActionLink(
string linkText,
string actionName,
string controllerName,
object routeValues,
object htmlAttributes
)
So in your case:
所以在你的情况下:
@Html.ActionLink("Download file", "GetFile", "MyController", new { startDate = "##" }, new { id="mydownloadlink" })
Using jQuery you can change the value of the startDate
in the link with the content of your date picker or textbox.
使用 jQuery,您可以使用startDate
日期选择器或文本框的内容更改链接中的值。
$("#mydownloadlink").attr("href").replace("##", $("#yourdatetexbox").val);
Then, in your controller, just use one of the other answers here, about FileResult
.
然后,在您的控制器中,只需使用此处的其他答案之一, about FileResult
。
Hope this help you...
希望这对你有帮助...