Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Upload file inside chrome extension

I need to add a browse button inside my Chrome extension. Users click it and choose a file. I then want to retrieve the file contents (bytes) and do some work on it.

I don't want to have to submit the file to a remote server and then get the response (if that's even doable from inside a Chrome extension), it should be all client-side.

Is this doable inside Chrome extensions?

like image 894
Ashraf Amayreh Avatar asked Jul 22 '14 11:07

Ashraf Amayreh


People also ask

Can a Chrome extension access local files?

For security reasons, by default the Chrome browser does not allow extensions to access local files. If you want the accelerator to access local files (locations of "file:///...", instead of "http://" or "https://"), you must configure Chrome to allow the access.

How much does it cost to upload a Chrome extension?

Before you publish your first app or extension from your account, you must pay a one-time $5 developer signup fee. A reminder in the dashboard will appear until you pay the fee. For more information, including troubleshooting tips, see the Registration article.


1 Answers

You should be looking at the FileReader API.

The FileReader object lets web applications asynchronously read the contents of files (or raw data buffers) stored on the user's computer, using File or Blob objects to specify the file or data to read.

A very good basic example of using this interface is in this question.

A minimal example: suppose that you have an <input type="file" id="file"> with a text file selected.

var file = document.getElementById("file").files[0];
var reader = new FileReader();
reader.onload = function(e){
  console.log(e.target.result);
}
reader.readAsText(file);

If you need methods other than reading as text (i.e. binary data), see the docs.

Also, this is a good overview: Using files from web applications

like image 158
Xan Avatar answered Sep 28 '22 01:09

Xan