Firefox忽略内容分解

发布于 2025-01-19 12:45:18 字数 839 浏览 3 评论 0原文

我正在尝试编写MVC端点,该端点将选择将内容分解设置为内联或附件,以便在新的浏览器选项卡中显示文件(pdf),或者以下载它。 UI允许用户选择他们希望如何打开文件(不是我的设计 - 无法更改其方面)。

请注意,这可以按预期的方式在Chrome/Edge中起作用。

在Firefox中,PDF的申请设置似乎胜过内容。是否有可靠的方法来尊重firefox?最好是一种可以使用浏览器安装的香草安装的方法,以便最终用户不需要对其进行任何修改才能工作。

这是我用来设置响应的代码(类是从apicontroller派生的):

var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Content = new PushStreamContent((stream, content, context) =>
{
    dispatcher.Dispatch(request, stream);
}, new MediaTypeHeaderValue(MediaTypeNames.Application.Pdf));


response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(contentDisposition)
{
    FileName = $"{auto_generated_fileName}.pdf",
};

response.Headers.CacheControl = new CacheControlHeaderValue()
{
    NoCache = true,
    NoStore = true
};

return response;

I'm trying to write MVC endpoint that will optionally set the content-disposition to inline or attachment in order to either display the file (a pdf) inside a new browser tab or else to download it. The UI allows the user to select how they'd like to open the file (not my design - can't change that aspect of it).

Note that this works in Chrome/Edge just as expected.

In Firefox, the application settings for PDF appear to trump the content-disposition. Is there a reliable way to get Firefox to respect the content-disposition? Preferably a way that will work w/ a vanilla installation of the browser such that end-users don't need to make any modifications on their end for it to work.

Here's the code I'm using to setup my response (class is derived from ApiController):

var response = Request.CreateResponse(System.Net.HttpStatusCode.OK);
response.Content = new PushStreamContent((stream, content, context) =>
{
    dispatcher.Dispatch(request, stream);
}, new MediaTypeHeaderValue(MediaTypeNames.Application.Pdf));


response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue(contentDisposition)
{
    FileName = 
quot;{auto_generated_fileName}.pdf",
};

response.Headers.CacheControl = new CacheControlHeaderValue()
{
    NoCache = true,
    NoStore = true
};

return response;

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

把人绕傻吧 2025-01-26 12:45:18

我们也在WebApp中注意到了这个问题。 WebApp具有一个下载按钮,可让用户下载PDF文件。 Firefox在当前选项卡中显示了PDF文件,该文件有效地杀死了WebApp。

经过一番研究之后,这似乎是一个有意的功能,请参见 firefox 98 的发行说明:

当您设置一个应用程序以打开Firefox偏好设置中特定类型的文件时,这些文件将自动打开,甚至由网站提供的文件“ content-disposition:attactment”。默认情况下,设置为在Firefox中打开的PDF文件也是如此。这是解决 bug 453455

就我个人而言,尽管我可以理解某些用户可能想要这个行为不佳的网页,但这是行为良好的Web应用程序的问题。

设置 noreferrer“似乎也无法正常工作,Firefox仍然显示文件内联(使用Firefox 99.0进行测试),

因此据我所知,如果浏览器不允许,您不能强迫浏览器下载文件。其他Web应用程序(例如OwnCloud或Google Drive)也存在相同的问题 - 如果您单击Google Drive中的PDF文件,然后单击dowload,Firefox仍然打开PDF文件,而Chrome则将下载。

目前,您能做的最好的方法是在新标签中打开文件,以防止WebApp或网页被下载的文件替换(这也是Google Drive似乎正在执行的操作)。您可以通过 target属性< a>链接或通过 formtarget atttribute < button>元素上。

We have noticed this issue in our webapp as well. The webapp has a download button that lets the user download a PDF file. Firefox shows the PDF file in the current tab, which effectively kills the webapp.

After a bit of research, this appears to be an intentional feature, see the release notes for Firefox 98:

When you set an application to open files of a specific type in your Firefox preference settings, those files will open automatically, even files served by the website with "content-disposition: attachment". The same applies to PDF files that are set to open in Firefox by default. This is a fix to bug 453455.

Personally, while I can understand some users may want this for web pages that don't behave well, this is an issue for well-behaved web apps.

Setting the download attribute on the anchor does not appear to work either, Firefox still shows the file inline (tested with Firefox 99.0)

So as far as I am aware, you cannot force the browser to download the file if the browser does not allow it. Other web apps such as OwnCloud or Google Drive are having the same issue -- if you click right on a PDF file in Google Drive and then click on Dowload, Firefox still open the PDF file inline, whereas Chrome downloads it.

For now, it seems the best you can do is to open file in a new tab, to prevent the webapp or web page from being replaced by the downloaded file (which is also what Google Drive seems to be doing). You can open the download in a new tab or window e.g. via the target attribute on an <a> links or via the formtarget atttribute on a <button> element.

尘曦 2025-01-26 12:45:18

在尝试找到与OP相同问题的解决方案后,我找到了 @Blutorange的答案。但是,就在我到达这里之前,我从2013年开始偶然发现了这个答案 - https://stackoverflow.com/a/a/16515146 建议将content-type标题设置为application/Ocket-stream,而不是application/pdf

我尝试了该解决方案,您知道什么 - 它有效! PDF会自动在Firefox的新标签中打开,但至少它不能替换我的应用程序的标签,所以! Chrome似乎也不介意,我在计算机上的PDF查看器也将文件识别为PDF。

现在,这可能不是我们面临的问题最“正确”的修复程序,但这是强迫打开新标签的替代方法。

I found @blutorange's answer after trying to find a solution to the same problem as OP. However, just before I got here, I stumbled across this answer from back in 2013 - https://stackoverflow.com/a/16515146 which suggests to set the Content-Type header to application/octet-stream, instead of application/pdf.

I tried that solution and what do you know - it works! The PDF opens in a new tab in Firefox automatically, but at least it doesn't replace the tab of my application, so yay! Chrome doesn't seem to mind it either and my PDF viewer on my computer also recognizes the files as PDFs.

Now, this might not be the most "correct" fix to the issue we're facing, but it's an alternative to forcing open a new tab.

做个ˇ局外人 2025-01-26 12:45:18

现在,浏览器忽略了内容类型:应用程序/八位字节和内容插词“附件”;范围。

浏览器查看内容分配中的文件名扩展名,以确定是否应该在浏览器窗口中打开文件,或弹出文件对话框询问用户是否以及他们要将文件保存到何处。

如果您想强制浏览器打开文件对话框,则可以放下文件名扩展名,以迫使浏览器谦虚并征求用户许可。

The browsers now ignore the Content-Type: application/octet-stream, and the Content-Disposition "attachment;" parameter.

The browsers look at the filename extension in Content-Disposition to determine if it should open the file in the browser window, or popup a file dialog asking the user if, and where they want to save the file to.

If you want to force the browser to open a file dialog then you can drop the filename extension which forces the browser to humble itself and ask for user permission.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文