1

如何FileResult使用 ajax 调用调用 MVC 项目中的控制器,以便在单击按钮时下载文件。我尝试了以下代码,但每次都会出错。如果我使用它调用它,@Ajax.ActionLink()我会成功,但我需要传输仅在我的 jquery 中而不是在我的 html 中获得的数据。

$('#export_to_excel').click(function () {
    var data = {
        "startIndex": "0",
        "no_of_days": $('#no_of_days').val(),
        "report_id": "A"
    };
    var no_days = $('#no_of_days').val()
    $.ajax({
        url: '@Url.Action("ExportToExcel", "ContentAudit",new { SPHostUrl =SharePointContext.GetSPHostUrl(HttpContext.Current.Request).AbsoluteUri })',
        type: 'POST',
        data: data,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            alert("success");
        },
        error: function (msg) {
            alert("Error");
        }
    });
});

控制器

public FileResult ExportToExcel(ExportExcel excel)
{
    return File(data1, Response.ContentType,fileName); 
}
4

0 回答 0