3

My goal is to upload a file in the background task with a file coming from the content script.

I figured it's not possible to pass a File object directly from content script to the background because chrome.runtime.sendMessage JSON serialize the data.

It's possible to pass a string url to the file with URL.createObjectURL, which works. But the URL is tied to the document in the window on which it was created (current tab content script). If I refresh, navigates away, close the tab those urls are destroyed.

Read the entire file with FileReader and keep in memory is not an option because it'll crash the extension on large files.

Questions:

  1. Keep the URL.createObjectURL alive?
  2. Pass File object to background?
  3. Upload a file in the background task with a file from the content script?
  4. Pass file link to popup and submit form there, which then run in background?
Jafu
  • 470
  • 2
  • 6
  • 19
  • Q1: Not possible. Q2 and Q3. Fully answered by https://stackoverflow.com/questions/24211073/chrome-extension-crashes-when-i-try-to-upload-a-big-file. Q4: What do you mean? – Rob W Jun 18 '14 at 19:59
  • Thanks @RobW, I used your answer from this thread http://stackoverflow.com/questions/21086918/does-chrome-runtime-support-posting-messages-with-transferable-objects/21101597#21101597 and by your input also made the object transferable by slicing it first. You can submit it as an answer if you want. – Jafu Jun 19 '14 at 09:18

1 Answers1

1

You can use a SharedWorker to create a communication channel between your content script and background script that allows you to transfer DOM objects such as Files. For an example, see the code in Does chrome.runtime support posting messages with transferable objects?

A quick test shows that it is indeed possible to send a File to the background, but also shows that its internal state is corrupted: You can read the contents of the file using FileReader, but you cannot upload the file through XMLHttpRequest.

I've found a work-around though: Assuming that your File is stored in the file variable, you can use file.slice(0, file.size) instead of file to get a Blob that is still backed by the file and usable in XMLHttpRequest even after passing it to the background page.

Community
  • 1
  • 1
Rob W
  • 341,306
  • 83
  • 791
  • 678