如何实现文件下载使用AJAX和MVC [英] How to implement file download with AJAX and MVC
问题描述
我想使用MVC下,jQuery的AJAX调用一些PARAMS提供文件下载操作
I would like to provide a file download operation by using the jQuery AJAX call with some params under MVC
例如
(javascript)
function DoDownload(startDate) {
$.ajax({
url:"controller/GetFile/",
data: {startDate:startDate}
...
});
}
C# Controller Code
public void GetFile(string startDate) {
var results = doQuearyWith(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.
如果我不使用AJAX,这将是一个preferred方式使用JavaScript来传递在PARAMS到MVC控制器?
If I dont use ajax, what would be a preferred way to pass in the params to the MVC controller using javascript?
例如:
window.location = "mycontroller/GetFile/" + $("#fromDate").val();
假设的日期2012年12月25日是 这会产生
assuming the date is 12-25-2012 would this produce
mycontroller/GetFile/12/25/2012
将MVC把这三个PARAMS?
would MVC treat this as three params?
推荐答案
我最后怎么了做的是从喜欢我的javascript调用我的控制器:
What I ended up doing is calling my controller from my javascript like:
VAR URL =/ myController的/的GetFile?的startDate =+ $(#指明MyDate)。VAL()+等等...
var url = "/mycontroller/GetFile?startDate=" + $("#mydate").val() + etc...
了window.location = URL;
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.
这篇关于如何实现文件下载使用AJAX和MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!