ysw*_*rer 1 httpresponse content-disposition response-headers asp.net-core-mvc .net-core
我正在尝试从我的服务器(使用 ASP.NET Core MVC)检索文件并将其显示在浏览器中。在浏览了此处和其他站点上的许多线程后,似乎每次都建议相同的解决方案;这是在响应标头中使用内联的 Content-Disposition。我的代码如下:
IFileInfo file = _fileProvider.GetFileInfo(filename);
Response.Headers.Add("Content-Disposition", $"inline; {file.Name}");
return File(file.CreateReadStream(), "application/pdf", file.Name);
Run Code Online (Sandbox Code Playgroud)
虽然这似乎是基于我见过的其他线程的正确代码,但 Content-Disposition 标头在我的代码中绝对没有任何作用。无论我有标题、删除它还是制作其他东西(即附件),它都会下载并保存文件,但除非我手动打开文件,否则不会显示它。
有谁知道我在这里想念什么?
返回File接受文件名参数的重载会将您的Content-Disposition标头覆盖为attachment; {filename}. 做就是了:
return File(file.CreateReadStream(), "application/pdf");
Run Code Online (Sandbox Code Playgroud)
你会没事的。
| 归档时间: |
|
| 查看次数: |
1705 次 |
| 最近记录: |