FileReader readAsText() async issues?

前端 未结 3 2112
忘掉有多难
忘掉有多难 2021-01-12 22:29

I have implemented the following code to parse a CSV via a selection:

export async function parse(file: File) {
           


        
3条回答
  •  渐次进展
    2021-01-12 23:22

    Here is the JSBin I have tried and it work like a charm.

    function parse(file) {
      const reader = new FileReader();
      reader.readAsText(file);
      reader.onload = function(event) {
        // The file's text will be printed here
      console.log(reader.result)
      }
    }
    

    Updated:

    I write you a Promise version.

    async function parse(file) {
      const reader = new FileReader();
      reader.readAsText(file);
      const result = await new Promise((resolve, reject) => {
        reader.onload = function(event) {
        resolve(reader.result)
        }
      })
      console.log(result)
    }
    

提交回复
热议问题