Transfer file to webworker: DataCloneError: The object could not be cloned
Asked Answered
T

2

11

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);

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]);

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.

And still

DataCloneError: The object could not be cloned.

in Firefox.

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

Thane answered 26/9, 2014 at 8:56 Comment(2)
There is Mozilla bug bugzilla.mozilla.org/show_bug.cgi?id=823484, so you are out of luck unless someone will fix it. Actually I am willing to fix, however I am not comfortable making Firefox builds, so if someone could make them fro me, then I could take care of codeSarong
I had the same problem, The object could not be cloned. That was because I was just refering to the param in my message. Can you provide more code?Blistery
I
10

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.

Improvident answered 8/11, 2014 at 10:51 Comment(0)
F
0

It seems now (2024, likely also earlier) File or Blob objects can be transfered today.

(What does not work are the responses from fetch. So either the data should be read in advance with blob() or the file url should be passed.)

Farman answered 20/3 at 21:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.