ASP.Net Core内容处置附件/内联 [英] ASP.Net Core Content-Disposition attachment/inline

查看:151
本文介绍了ASP.Net Core内容处置附件/内联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从WebAPI控制器返回文件. Content-Disposition标头值自动设置为附件".例如:

I am returning a file from a WebAPI controller. The Content-Disposition header value is automatically set to "attachment". For example:

性格:依恋; filename ="30956.pdf";文件名* = UTF-8''30956.pdf

Disposition: attachment; filename="30956.pdf"; filename*=UTF-8''30956.pdf

设置为附件时,浏览器将要求保存文件而不是打开文件.我想要打开它.

When it is set to attachment the browser will ask to save file instead of opening it. I would like it to open it.

如何将其设置为内联"而不是附件"?

How can I set it to "inline" instead of "attachment"?

我正在使用这种方法发送文件:

I am sending the file using this method:

public IActionResult GetDocument(int id)
{
    var filename = $"folder/{id}.pdf";
    var fileContentResult = new FileContentResult(File.ReadAllBytes(filename), "application/pdf")
    {
        FileDownloadName = $"{id}.pdf"
    };
    // I need to delete file after me
    System.IO.File.Delete(filename);

    return fileContentResult;
}

推荐答案

我发现最好的方法是手动添加content-disposition标头.

The best way I have found is to add the content-disposition headers manually.

private IActionResult GetFile(int id)
{
       var file = $"folder/{id}.pdf";

       // Response...
       System.Net.Mime.ContentDisposition cd = new System.Net.Mime.ContentDisposition
       {
              FileName = file,
              Inline = displayInline  // false = prompt the user for downloading;  true = browser to try to show the file inline
       };
       Response.Headers.Add("Content-Disposition", cd.ToString());
       Response.Headers.Add("X-Content-Type-Options", "nosniff");

       return File(System.IO.File.ReadAllBytes(file), "application/pdf");
}

这篇关于ASP.Net Core内容处置附件/内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆