错误:资源被解释为Document但是使用MIME类型application/pdf进行传输

saa*_*adq 8 javascript pdf ajax mime-types ecmascript-6

我正在从我的服务器向客户端发送PDF流,然后<object>在客户端的标签中显示该PDF .这是我的代码:

server.js

router.get('/pdf', function * () {
  var stream = getMyFileStream();
  this.set('Content-Type', 'application/pdf');
  this.response.body = stream;
});
Run Code Online (Sandbox Code Playgroud)

client.js

var objectElement = document.querySelector('object');

fetch('/pdf', request)
  .then(res => res.blob())
  .then(blob => URL.createObjectURL(blob))
  .then(url => {
    objectElement.setAttribute('data', url)
    objectElement.setAttribute('type', 'application/pdf')
  })
Run Code Online (Sandbox Code Playgroud)

此代码似乎正常工作,但我在控制台中收到以下警告:

资源解释为Document但使用MIME类型application/pdf进行传输

为什么它认为我的资源应该是text/html?如果我将Content-Type标题更改为text/html,则会使警告消失但显然会导致PDF的呈现问题.任何帮助,将不胜感激.

小智 1

这很可能是因为有来自 /pdf 的重定向和/或没有文件扩展名。

添加这个额外的标头:

this.set('Content-Disposition', 'attachment; filename=results.pdf');
Run Code Online (Sandbox Code Playgroud)