【问题标题】:Transfer file to webworker: DataCloneError: The object could not be cloned将文件传输到 webworker:DataCloneError:无法克隆对象
【发布时间】:2014-11-21 05:43:51
【问题描述】:

我想将文件从表单传输到网络工作者。在 chrome 中,我可以简单地使用此代码来传输 FileList-Object:

worker.postMessage(files: array_files);

但是使用 Firefox 我得到这个错误:

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

所以我尝试将语法用于可转移对象。像这样?

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

但是我在 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 传递给工作人员的正确方法是什么?

【问题讨论】:

  • 存在 Mozilla 错误bugzilla.mozilla.org/show_bug.cgi?id=823484,所以除非有人修复它,否则你很不走运。实际上我愿意修复,但是我不喜欢制作 Firefox 版本,所以如果有人可以帮我制作它们,那么我可以处理代码
  • 我遇到了同样的问题,The object could not be cloned. 那是因为我只是在我的消息中引用了参数。能否提供更多代码?

标签: file web-worker pass-data


【解决方案1】:

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

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

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

另外请注意,将类型化数组作为可转移对象传递给另一个工作程序/窗口会使发送的工作程序/窗口无法访问转移的数组。

【讨论】:

    猜你喜欢
    • 2020-05-01
    • 2020-05-06
    • 2017-11-07
    • 1970-01-01
    • 2011-01-20
    • 1970-01-01
    • 2020-10-22
    • 2011-05-27
    相关资源
    最近更新 更多