如何通过asp.net MVC 4中的ajax请求下载文件

roh*_*ngh 11 jquery json asp.net-ajax asp.net-mvc-4

以下是我的代码:

ActionResult DownloadAttachment(student st)
{          
    var file = db.EmailAttachmentReceived.FirstOrDefault(x => x.LisaId == st.Lisaid);

    byte[] fileBytes = System.IO.File.ReadAllBytes(file.Filepath);
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, file.Filename);                 
}
Run Code Online (Sandbox Code Playgroud)

这是我正在使用的脚本

$(function () {
    $("#DownloadAttachment").click(function () {
        $.ajax({
            url: '@Url.Action("DownloadAttachment", "PostDetail")',
            contentType: 'application/json; charset=utf-8',
            datatype: 'json',
            type: "GET",
            success: function () {
                alert("sucess");
            }
        });    
    });
});      
Run Code Online (Sandbox Code Playgroud)

如何返回文件下载追求上面的代码?

Gov*_*har 7

我认为不需要Ajax调用你可以简单地使用超链接,如下例所示.

查看代码

<a href="@Url.Action("DownloadAttachment", "PostDetail", new { studentId = 123 })">Download Form</a>
Run Code Online (Sandbox Code Playgroud)

控制器方法

public ActionResult DownloadAttachment(int studentId)
{          
    // Find user by passed id
    var file = db.EmailAttachmentReceived.FirstOrDefault(x => x.LisaId == studentId);    
    byte[] fileBytes = System.IO.File.ReadAllBytes(file.Filepath);    
    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, file.Filename);                           
}
Run Code Online (Sandbox Code Playgroud)

  • 这个例子对我有用。此示例发送 HttpGet 请求,因此请求中的数据负载(仅一个记录 ID)作为参数包含在 URL 中。但是,如果我想发送 HttpPost 请求,并将数据负载作为对象(包含许多字段)包含在请求中,该怎么办? (2认同)

Edi*_*vić 5

请在ajax成功中试试这个

success: function () {
    window.location = '@Url.Action("DownloadAttachment", "PostDetail")';
}
Run Code Online (Sandbox Code Playgroud)

更新的答案:

public ActionResult DownloadAttachment(int studentId)
{          
    // Find user by passed id
    // Student student = db.Students.FirstOrDefault(s => s.Id == studentId);

    var file = db.EmailAttachmentReceived.FirstOrDefault(x => x.LisaId == studentId);

    byte[] fileBytes = System.IO.File.ReadAllBytes(file.Filepath);

    return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, file.Filename);                       

}
Run Code Online (Sandbox Code Playgroud)

Ajax请求:

$(function () {
        $("#DownloadAttachment").click(function () {
            $.ajax(
            {
                url: '@Url.Action("DownloadAttachment", "PostDetail")',
                contentType: 'application/json; charset=utf-8',
                datatype: 'json',
                data: {
                    studentId: 123
                },
                type: "GET",
                success: function () {
                    window.location = '@Url.Action("DownloadAttachment", "PostDetail", new { studentId = 123 })';
                }
            });

        });
    });
Run Code Online (Sandbox Code Playgroud)

  • 根本没有必要使用ajax.只需直接调用window.location即可.所有这一切都是两次提出相同的请求.我不知道为什么会被投票赞成. (14认同)
  • 这将调用DownloadAttachment方法2次.怎么避免呢? (4认同)
  • 基本上如果我们不给ajax调用仍然可以通过window.location下载文件只是因为我们需要ajax请求我们在成功函数中使用相同的感谢帮助. (3认同)
  • 对DownloadAttachment方法没有双重访问权限? (2认同)
  • 这里面有一个多余的ajax调用。您将相同的数据传递两次。问题是 ajax 被设计为无法下载文件。 (2认同)