File transfer in webworker: DataCloneError: object cannot be cloned

I want to transfer a file from a form to a web worker. In chrome i simple, you 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 using Syntax for portable objects. Something like that?

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

But with this I get it in Chrome

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

And further

 DataCloneError: The object could not be cloned. 

in Firefox.

What is the correct way to pass a FileList to an employee?

+6
source share
1 answer

I don’t know how to transfer File objects with postMessage, but at least I can tell that portable objects do not work this way. An additional second parameter is an array of ArrayBuffer instances to back up any typed arrays that you want to pass. For example, suppose the message you want to post is a structured object:

 var message = {foo: 'abc', bar: new Uint8Array(...)}; worker.postMessage(message, [message.bar.buffer]) 

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

+4
source

Source: https://habr.com/ru/post/975873/


All Articles