【问题标题】:Download File C# MVC from byte[]从 byte[] 下载文件 C# MVC
【发布时间】:2020-05-27 13:30:19
【问题描述】:

我正在尝试从字节数组下载文件,但提示似乎没有进行下载。我是否需要包含额外的 ContentDisposition 属性?如果我查看 IE 中的网络流量,我可以看到文件请求有效并且返回 200,此外我还可以从 IE 调试工具内容下载文件。

字节数组中存储的文件是Word文档。我已将 mime 类型设置为:

application/vnd.openxmlformats-officedocument.wordprocessingml.document

文档文件名为:QuickStartGuide.docx

以及为什么没有显示下载提示?

[HttpPost]
[ValidateAntiForgeryToken]
public FileContentResult DocumentDownload(int documentId)
{
    try
    {
        var document = BusinessLayer.GetDocumentsByDocument(documentId, AuthenticationHandler.HostProtocol).FirstOrDefault();

        System.Net.Mime.ContentDisposition contentDisposition = new System.Net.Mime.ContentDisposition();

        contentDisposition.FileName = document.FileName;
        contentDisposition.Inline = false;

        var result = new FileContentResultWithContentDisposition(document.FileBytes, document.FileType, contentDisposition);

        return result;
    }
    catch
    {
        throw;
    }
}


public class FileContentResultWithContentDisposition : FileContentResult
{
    private const string ContentDispositionHeaderName = "Content-Disposition";

    public FileContentResultWithContentDisposition(byte[] fileContents, string contentType, ContentDisposition contentDisposition)
        : base(fileContents, contentType)
    {
        // check for null or invalid ctor arguments
        ContentDisposition = contentDisposition;
    }

    public ContentDisposition ContentDisposition { get; private set; }

    public override void ExecuteResult(ControllerContext context)
    {
        // check for null or invalid method argument
        ContentDisposition.FileName = ContentDisposition.FileName ?? FileDownloadName;
        var response = context.HttpContext.Response;
        response.ContentType = ContentType;
        response.AddHeader(ContentDispositionHeaderName, ContentDisposition.ToString());
        WriteFile(response);
    }
}

【问题讨论】:

    标签: c# .net asp.net-mvc-5


    【解决方案1】:

    你的action方法修饰为POST,但是文件下载有GET操作,下载也不需要防伪验证。

    ASP.NET MVC 框架已经内置了FileResult。 MVC Controller 本身自带了便利功能File(...)(https://msdn.microsoft.com/en-us/library/system.web.mvc.controller.file(v=vs.118).aspx)

    为了通知浏览器下载文件,您必须指定内容类型和下载文件名。这会将您的代码缩短为:

    [HttpGet]
    public FileResult DocumentDownload(int documentId)
    {
        var document = BusinessLayer.GetDocumentsByDocument(documentId, AuthenticationHandler.HostProtocol).FirstOrDefault();
    
        return File(document.FileBytes, document.FileType, document.FileName);           
    }
    

    【讨论】:

    • 如果文档对象为空怎么办?
    【解决方案2】:

    FileResult 可能更适合你。指定您的内容类型和文件名,我认为这就足够了。

    public FileResult Download()
    {
        var document = BusinessLayer.GetDocumentsByDocument(documentId, AuthenticationHandler.HostProtocol).FirstOrDefault();
        string fileName = document.FileName;
        return File(document.FileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }
    

    我使用了一个通用的 application-octet mediatype,你绝对可以使用你自己的。

    更多解决问题的方法,请查看herehere

    【讨论】:

      【解决方案3】:

      尝试添加“application/force-download”作为“content-type”标头的值,如下所述:https://stackoverflow.com/a/3007668/5592113

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2012-12-18
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-29
        相关资源
        最近更新 更多