How to avoid duplicate content-disposition headers with MVC3 FileContentResult?

前端 未结 1 1547
忘掉有多难
忘掉有多难 2020-12-16 16:34

We have some files stored in sql database. On an ASP.NET MVC3 form, we display 2 links:

View this file | Download this file

These links go to these correspon

相关标签:
1条回答
  • 2020-12-16 17:18

    When you use the overload File(byte[] contents, string mimeType, string fileName) a Content-Disposition header is automatically added to the response with attachment, so you don't need to add it a second time. For inline you could use the following overload File(byte[] contents, string mimeType) and manually add the Content-Disposition header:

    [ActionName("display-file")]
    public virtual ActionResult DisplayFile(Guid fileId)
    {
        var file = _repos.GetFileInfo(fileId);
        var cd = new ContentDisposition
        {
            Inline = true,
            FileName = file.Name
        };
        Response.AddHeader("Content-Disposition", cd.ToString()); 
        return File(file.Content, file.MimeType);
    }
    
    [ActionName("download-file")]
    public virtual ActionResult DownloadFile(Guid fileId)
    {
        var file = _repos.GetFileInfo(fileId);
        return File(file.Content, file.MimeType, file.Name);
    }
    
    0 讨论(0)
提交回复
热议问题