6

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?

ted
  • 13,596
  • 9
  • 65
  • 107
Ashraf Amayreh
  • 163
  • 2
  • 6

2 Answers2

9

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

Community
  • 1
  • 1
Xan
  • 74,770
  • 16
  • 179
  • 206
0

Regarding your question it is totally feasible to load and process a file within an extension. I implemented it using message passing https://developer.chrome.com/docs/extensions/mv3/messaging/.

Here is an example of how you can implement it, in my case I used the input file to load an excel. This is my public repo. https://github.com/juanmachuca95/gomeetplus

  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/33339255) – Lord-JulianXLII Dec 08 '22 at 14:41