How to implement file download with AJAX and MVC

后端 未结 4 1773
Happy的楠姐
Happy的楠姐 2021-02-07 20:06

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

Example

(javascript)
function DoDownload(start         


        
4条回答
  •  清酒与你
    2021-02-07 20:55

    Using the ActionLink helper, you can pass multiple params to your controller:

    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.

    $("#mydownloadlink").attr("href").replace("##", $("#yourdatetexbox").val);
    

    Then, in your controller, just use one of the other answers here, about FileResult.

    Hope this help you...

提交回复
热议问题