Upload file inside chrome extension

后端 未结 1 547
再見小時候
再見小時候 2021-01-14 08:58

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.

<
相关标签:
1条回答
  • 2021-01-14 09:39

    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

    0 讨论(0)
提交回复
热议问题