将文件传输到Webworker:DataCloneError:无法克隆对象 [英] Transfer file to webworker: DataCloneError: The object could not be cloned

查看:925
本文介绍了将文件传输到Webworker:DataCloneError:无法克隆对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将文件从表单传输到Webworker.在chrome中,我可以简单地使用以下代码来传输FileList-Object:

I want to transfer a file from a form to a webworker. In chrome i simple can use this code to transfer a FileList-Object:

worker.postMessage(files: array_files);

但是使用Firefox时,出现此错误:

But with Firefox i get this error:

Transfer file to webworker: DataCloneError: The object could not be cloned.

因此,我尝试对可转让对象使用语法.像这样吗?

So i tried to use the Syntax for transferable objects. Something like this?

var post = {files: array_files, file_ids: response.file_ids};
worker.postMessage(post, [post]);

但是有了它,我在Chrome浏览器中得到了

But with that i get this in Chrome

Uncaught DataCloneError: Failed to execute 'postMessage' on 'Worker': Value at index 0 does not have a transferable type.

还有

DataCloneError: The object could not be cloned.

在Firefox中.

将FileList传递给工作人员的正确方法是什么?

What is the right way to pass a FileList to a worker?

推荐答案

我不知道如何通过postMessage传递File对象,但是至少我可以建议可移植对象不能以这种方式工作.可选的第二个参数是要传递的任何类型的数组的支持ArrayBuffer实例的数组.例如,假设您要发布的消息是结构化对象:

I don't know how to pass File objects with postMessage, but at the least I can advise that transferable objects do not work in this way. The optional second parameter is an array of the backing ArrayBuffer instances of any typed arrays you wish to pass. So for instance, suppose the message you would like to post is a structured object:

var message = {foo: 'abc', bar: new Uint8Array(...)};

worker.postMessage(message, [message.bar.buffer])

还请注意,将类型化数组作为可转移对象传递给另一个工作器/窗口将使发送的数组无法从发送工作器/窗口访问.

Also notice that passing a typed array to another worker/window as a transferable object makes the transferred array inaccessible from the sending worker/window.

这篇关于将文件传输到Webworker:DataCloneError:无法克隆对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆