Javascript read file without using input

前端 未结 3 795
天涯浪人
天涯浪人 2020-12-01 19:00

I have this code and for a file to be converted into base64 I have to click on Choose file and then select it. I want to hardcode the file name so it is converted to base64

3条回答
  •  星月不相逢
    2020-12-01 19:06

    You can launch chromium, chrome browser with --allow-file-access-from-files flag set, use fetch() of XMLHttpRequest() to request file from local filesystem.

    fetch("file:///path/to/file")
    .then(response => response.arrayBuffer())
    .then(ab => {
      // do stuff with `ArrayBuffer` representation of file
    })
    .catch(err => console.log(err));
    

    See also Read local XML with JS

提交回复
热议问题