如何使用AJAX和MVC实现文件下载

时间:2022-10-05 14:31:40

I would like to provide a file download operation by using the jQuery AJAX call with some params under MVC

我想通过使用jQuery AJAX调用提供一个文件下载操作,并在MVC下使用一些params

example

例子

(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.

但在上面的例子中,日期是动态的。

If I dont use ajax, what would be a preferred way to pass in the params to the MVC controller using javascript?

如果我不使用ajax,那么如何使用javascript将params传递给MVC控制器呢?

example:

例子:

window.location  = "mycontroller/GetFile/" + $("#fromDate").val();

assuming the date is 12-25-2012 would this produce

假设日期是12-25-2012年

mycontroller/GetFile/12/25/2012

would MVC treat this as three params?

MVC会将其视为三个参数吗?

4 个解决方案

#1


11  

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.

我最初关心的是日期参数。我不想去解析它。

#2


3  

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.

然后,在你的控制器中,使用另一个关于FileResult的答案。

Hope this help you...

希望这有助于你…

#3


2  

You can use the File method of controller class to return a file back to the browser.

可以使用controller类的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 overloads of the File method and use appropriate one.

您还可以从物理位置(路径)或流返回文件。检查文件方法的所有重载,并使用适当的方法。

This has nothing to do with ajax. this is normal GET request over a browser.

这与ajax无关。这是通过浏览器的正常GET请求。

#4


1  

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

处理一个ASP。NET MVC FileResult返回一个(jQuery) Ajax调用

File download in Asp.Net MVC 2

在Asp文件下载。Net MVC 2

I hope this helps.

我希望这可以帮助。

#1


11  

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.

我最初关心的是日期参数。我不想去解析它。

#2


3  

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.

然后,在你的控制器中,使用另一个关于FileResult的答案。

Hope this help you...

希望这有助于你…

#3


2  

You can use the File method of controller class to return a file back to the browser.

可以使用controller类的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 overloads of the File method and use appropriate one.

您还可以从物理位置(路径)或流返回文件。检查文件方法的所有重载,并使用适当的方法。

This has nothing to do with ajax. this is normal GET request over a browser.

这与ajax无关。这是通过浏览器的正常GET请求。

#4


1  

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

处理一个ASP。NET MVC FileResult返回一个(jQuery) Ajax调用

File download in Asp.Net MVC 2

在Asp文件下载。Net MVC 2

I hope this helps.

我希望这可以帮助。