简体   繁体   English

在blob上设置内容类型

[英]Set content-type on blob

We're transferring a Blob (image) down a websocket and rendering it to a canvas on the other end. 我们将Blob (图像)传输到websocket并将其渲染到另一端的画布上。

When I use createObjectURL with the blob, I get this warning: 当我使用带有blob的createObjectURL时,我收到此警告:

Resource interpreted as Image but transferred with MIME type text/plain: "blob:https%3A//example.com/demo".

We create the object URL using the following code. 我们使用以下代码创建对象URL。 The blob is send via a standard websocket with socket.binaryType = "blob"; blob通过带有socket.binaryType = "blob";的标准websocket发送socket.binaryType = "blob"; on the client side: 在客户端:

socket.onmessage = function(e) {
  var blob = e.data;
  var url = (window.URL || window.webkitURL).createObjectURL(blob);

  var image = document.createElement('img');
  image.src = url;
}

The only way I can think to address this warning is to create a copy of the blob with the following code, but I don't want to introduce the overhead of copying all the data: 我能想到解决此警告的唯一方法是使用以下代码创建blob的副本,但我不想引入复制所有数据的开销:

var blob = new Blob([e.data], {
  type: 'image/gif'
});

The method gets called dozens of times per second. 该方法每秒被调用几十次。

Any ideas on how to set the blob content-type without creating a duplicate Blob object with new Blob ? 关于如何设置blob内容类型而不创建具有new Blob的重复Blob对象的任何想法?

Let's consider you have Blob instance ( blob ). 让我们考虑你有Blob实例( blob )。 You can use then slice method on it: 你可以使用then slice方法:

blob = blob.slice(0, blob.size, "image/jpeg")

and that's it. 就是这样。

It just creates new blob with the same data, but with new type. 它只是创建具有相同数据的新blob,但具有新类型。

First I would wonder if when you say "error" you actually mean "warning". 首先,我想知道当你说“错误”时,你实际上是指“警告”。 They are really two different things and the browser treats them differently (it usually only tracks/raises warnings when the developer tools are open etc). 它们实际上是两个不同的东西,浏览器以不同的方式处理它们(它通常只在开发人员工具打开时跟踪/引发警告等)。

So first I would challenge the premise that this is even an issue ( the overhead of the browser "auto-typing" the blob versus the overhead of "newing" up a Blob etc ). 所以首先我要挑战的前提是这甚至是一个问题(浏览器“自动键入”blob的开销与“刷新Blob等”的开销)。

But, that said, the blob.type property is indeed inmutable in JavaScript and as such you have to set it when the blob is "newed". 但是,那就是说,blob.type属性确实在JavaScript中是不可变的,因此你必须在blob“newed”时设置它。 In your case it sounds like you are getting the data from a Objective-C socket and just daisy chaining it down via: 在您的情况下,听起来您正在从Objective-C套接字获取数据,并通过以下方式将其菊花链接下来:

ws.send(fromObjectiveCSocket, {binary: true, mask: true});

The blob data itself from the Objective-C socket is not containing the "header" type data of the type when it sends it across, and it sounds like your node is not touching the blob at all (have you tried decorating the new Blob in your node and then sending that down the socket to see if it retains the typing?). 来自Objective-C套接字的blob数据本身在发送它时不包含该类型的“标题”类型数据,并且听起来你的节点根本没有触及blob(你试过装饰新的Blob吗?您的节点,然后将其发送到套接字以查看它是否保留键入?)。

So what is happening is that the websocket is sending down just the blob data as it got it and when the receiving javascript gets it, it is implicitly typing it with a new Blob right then and there, just with a blank type. 所以正在发生的事情是websocket正在发送blob数据,因为它得到它,当接收到的javascript得到它时,它隐含地用新的Blob键入它然后就在那里,只是一个空白类型。

So essentially no, there does not seem to be any way around the new Blob construction if you really want to get rid of this warning. 所以基本上没有,如果你真的想要摆脱这个警告,似乎没有任何办法绕过新的Blob构造。 Even if you tried tricks like adding the type into the blob data and then splicing it out etc, you still can't get around the websocket receiving code implicitly typing it as a blob with a blank type. 即使您尝试了将类型添加到blob数据中然后将其拼接出来等技巧,您仍然无法绕过websocket接收代码,隐式地将其键入为具有空白类型的blob。

"Blob URLs only work with GET requests, and when one is successfully requested, the browser sends an HTTP 200 OK status code and also sends a Content-Type header that uses the type property of the Blob." “Blob URL仅适用于GET请求,当成功请求时,浏览器会发送HTTP 200 OK状态代码,并发送使用Blob类型属性的Content-Type标头。”

 var bb = new BlobBuilder();
 var blob = bb.getBlob("x-optional/mime-type-here");

 //send via websocket
 //load via websocket     

Am I completely off here? 我完全离开了吗?

I've gotten all of this from https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-22/blobs 我从https://www.inkling.com/read/javascript-definitive-guide-david-flanagan-6th/chapter-22/blobs得到了所有这些

HTML5 Rocks seems to be making a new Blob with the response from the XHR request. HTML5 Rocks似乎正在使用XHR请求的响应创建一个新的Blob。 So maybe it isn't so bad after all. 所以也许它毕竟不是那么糟糕。 http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-response http://www.html5rocks.com/en/tutorials/file/xhr2/#toc-response

Just throwing out some ideas. 只是抛出一些想法。

您可能应该在发送之前在服务器端设置Content-Type

You can solve this problem from the end you are delivering the image. 您可以从提供图像的最后解决此问题。 This problem is happening because of the wrong content-type, so you should configure your server to send image with image headers. 由于内容类型错误,会出现此问题,因此您应将服务器配置为使用图像标头发送图像。

If this was in PHP, you should have something like 如果这是在PHP中,你应该有类似的东西

header("Content-Type: image/jpeg");
echo $image_blob;
exit;

(Just giving you an idea) (只是给你一个想法)

In case of Node.js, though I am not an expert you can set the content type on the response object like: 对于Node.js,虽然我不是专家,但您可以在响应对象上设置内容类型,如:

app.get('/image.gif', function(req, res) {
  res.set('Content-Type', 'image/gif');
  res.sendfile('./image.gif');
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM