ASP.Net核心内容 – 处置附件/内联

我从WebAPI控制器返回一个文件。 Content-Disposition标头值自动设置为“attachment”。 例如:

性格:依恋; 文件名= “30956.pdf”; 文件名* = UTF-8”30956.pdf

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

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

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

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; } 

我找到的最好方法是手动添加内容处理标头。

 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"); } 

鉴于您不希望在字节数组中一次读取内存中的文件(使用各种File(byte[]...)重载或使用FileContentResult ),您可以使用File(Stream, string, string) overload,其中最后一个参数指示文件的下载名称:

 return File(stream, "content/type", "FileDownloadName.ext"); 

或者,您可以利用支持流的现有响应类型(例如FileStreamResult ,并FileStreamResult设置内容处置。 如FileResultExecutorBase ,执行此操作的规范方法是在操作方法中自己在响应上设置标头:

 // Set up the content-disposition header with proper encoding of the filename var contentDisposition = new ContentDispositionHeaderValue("attachment"); contentDisposition.SetHttpFileName("FileDownloadName.ext"); Response.Headers[HeaderNames.ContentDisposition] = contentDisposition.ToString(); // Return the actual filestream return new FileStreamResult(@"path\to\file", "content/type"); 

由于File()会忽略Content-Disposition我使用了这个:

 Response.Headers[HeaderNames.ContentDisposition] = new MimeKit.ContentDisposition { FileName = fileName, Disposition = MimeKit.ContentDisposition.Inline }.ToString(); return new FileContentResult(System.IO.File.ReadAllBytes(filePath), "application/pdf"); 

它的工作原理:-)

尝试使用HttpResponseMessage

 public IActionResult GetDocument(int id) { var filename = $"folder/{id}.pdf"; Response.Headers["Content-Disposition"] = $"inline; filename={id}.pdf"; var fileContentResult = new FileContentResult(System.IO.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版本和AspNetCore.Mvc ,我发现以前的答案都不可接受。 对我来说,简单地省略File的filename参数足以触发内联内容处理。

 return File(fileStream, contentType, fileName); // attachment return File(fileStream, contentType); // inline 

使用与ashley-lee类似的方法的Asp.Net MVC方法

注意: Chrome会下载附件。 请参见Ctrl-J列表。 但是,如果用户选择“打开”,它将在“浏览器”中打开,用户必须选择“在系统查看器中打开”。 例如,PDF签名字段在基于浏览器的PDF查看器中不可见。

 [HttpGet] public ActionResult GenericForm() { return new DownloadFileAsAttachmentResult(@"GenericForm.pdf", @"\Content\files\GenericForm.pdf", "application/pdf"); } public class DownloadFileAsAttachmentResult : ActionResult { private string _filenameWithExtension { get; set; } private string _filePath { get; set; } private string _contentType { get; set; } // false = prompt the user for downloading; true = browser to try to show the file inline private const bool DisplayInline = false; public DownloadFileAsAttachmentResult(string FilenameWithExtension, string FilePath, string ContentType) { _filenameWithExtension = FilenameWithExtension; _filePath = FilePath; _contentType = ContentType; } public override void ExecuteResult(ControllerContext context) { HttpResponseBase response = context.HttpContext.Response; response.Buffer = false; response.ContentType = _contentType; response.AddHeader("Content-Disposition", "attachment; filename=" + _filenameWithExtension); // force download response.AddHeader("X-Content-Type-Options", "nosniff"); response.TransmitFile(_filePath); } }