Html 资源被解释为文档但使用 MIME 类型应用程序/pdf 传输

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/39620917/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:50:10  来源:igfitidea点击:

Resource interpreted as Document but transferred with MIME type application/pdf

asp.net-mvchtmlpdf

提问by Matheus Miranda

See the following code:

请参阅以下代码:

Controller:

控制器:

public ActionResult GetPDF()
{
    byte[] pdf = GetPdfFromDatabase();
    return new FileContentResult(reportData, "application/pdf");
}

View:

看法:

<iframe src="@Url.Action("GetPDF","Account")" width="600" height="500"></iframe>

Javascript Console:

Javascript控制台:

enter image description here

在此处输入图片说明

Each time you load a page, show this warning !!!

每次加载页面时,显示此警告!!!

采纳答案by EThaizone Jo

For who come across this topic. (Me too.) For best solution is you need to create HTML page to act as iframe landing page to show PDF content.

对于谁遇到这个话题。(我也是。)最佳解决方案是您需要创建 HTML 页面作为 iframe 登录页面来显示 PDF 内容。

This is example html. I just extract from chrome.

这是示例 html。我只是从铬中提取。

<html><body style="background-color: rgb(38, 38, 38); height: 100%; width: 100%; overflow: hidden; margin: 0px;"><embed width="100%" height="100%" name="plugin" id="plugin" src="your_pdf_url" type="application/pdf"></body></html>

Just replace your_pdf_urlto your real url. I just add this html as middleware in Laravel and show this HTML so Chrome will not warning anymore. You can use this html as base and create better pdf landing page such as custom loading.

只需替换your_pdf_url为您的真实网址即可。我只是将此 html 添加为 Laravel 中的中间件并显示此 HTML,因此 Chrome 不会再发出警告。您可以使用此 html 作为基础并创建更好的 pdf 登录页面,例如自定义加载。

回答by Burak SARICA

An iframe requests the resource mentioned in src attribute with a content type text/html. So this message only says "I requested html but server responded with pdf"

iframe 请求 src 属性中提到的资源,内容类型为 text/html。所以这条消息只说“我请求了 html 但服务器用 pdf 响应”

回答by Márcio Rossato

I believe the warning can't be ignored.

我相信警告不能被忽视。

An alternative I found to this problem, use the object tag, like this:

我发现这个问题的另一种方法是使用 object 标签,如下所示:

<object data="your-data-base-64" type="application/pdf"></object>

<object data="your-data-base-64" type="application/pdf"></object>

and if you using angularJS, to prevent error on console do:

如果您使用 angularJS,为了防止控制台出错,请执行以下操作:

<object ng-attr-data="your-data-base-64" type="application/pdf"></object>

<object ng-attr-data="your-data-base-64" type="application/pdf"></object>