响应 header Content-Disposition 没有做任何事情

Response header Content-Disposition not doing anything

我正在尝试从我的服务器(使用 ASP.NET Core MVC)检索文件并将其显示在浏览器中。在浏览了这里和其他站点上的许多线程之后,似乎每次都提出了相同的解决方案;这是在响应 header 中使用 Content-Disposition 内联。我的代码如下:

IFileInfo file = _fileProvider.GetFileInfo(filename);
Response.Headers.Add("Content-Disposition", $"inline; {file.Name}");
return File(file.CreateReadStream(), "application/pdf", file.Name);

虽然这似乎是基于我见过的其他线程的正确代码,但 Content-Disposition header 在我的代码中绝对没有任何作用。无论我有 header、删除它还是制作其他东西(即附件),它都会下载并保存文件但不会显示它,除非我手动打开文件。

有人知道我在这里错过了什么吗?

返回接受文件名参数的 File 重载会将您的 Content-Disposition header 覆盖为 attachment; {filename}。只要做:

return File(file.CreateReadStream(), "application/pdf");

你会没事的。