ASP.Net 核心内容处理附件/内联 [英] ASP.Net Core Content-Disposition attachment/inline
问题描述
我正在从 WebAPI 控制器返回一个文件.Content-Disposition 标头值自动设置为附件".例如:
I am returning a file from a WebAPI controller. The Content-Disposition header value is automatically set to "attachment". For example:
处置:附件;文件名="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;
}
推荐答案
使用 AspNetCore
和 AspNetCore.Mvc
的 2.0.0 版,我没有找到以前的答案可以接受.对我来说,只需将文件名参数省略给 File
就足以触发内联内容处置.
With version 2.0.0 of AspNetCore
and AspNetCore.Mvc
, I found none of the previous answers to be acceptable. For me, simply ommitting the filename argument to File
was enough to trigger an inline content disposition.
return File(fileStream, contentType, fileName); // attachment
return File(fileStream, contentType); // inline
这篇关于ASP.Net 核心内容处理附件/内联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!