I want to let the user to select a file and then the extension to read it and upload. But I don't know how to do that in background script of the extension. I need to process the file in the background script because I use extensions APIs. I know how to do that in content script, i just create an input element with button and then i can easily check onchange event of the input. But I don't know how to let the user to select a file and read it in background script, please help me with this question.
-
1While not a duplicate, read through [this answer](http://stackoverflow.com/a/21138567/934239), this should help you. – Xan Jun 13 '14 at 09:05
1 Answers
It really depends on how you want the user to select that file.
Surfacing your element through a content script is awkward because you'd need to change the UI of some arbitrary page. An alternative is to put it in an extension popup, options pages, etc (anything with a chrome-extension:// scheme).
Content script mostly limits you to sending the content over chrome.runtime.sendMessage.
Extension page gives you more flexibility since it can at least share JS objects with the background page, so you can pass the actual File back. That said, Files are bound to the lifetime of the document, so when the popup goes away, or options page closes, you'll still lose that object. But at least you can read in its content efficiently and do whatever processing you need in the background page.

- 654
- 5
- 3