使用input file实现文件上传

    const input = document.createElement('input') as HTMLInputElement;
    input.type = 'file';
    input.accept = 'application/json';
    input.addEventListener('change', () => {
      const fileList = input.files;
      if (fileList?.length !== 1) {
        console.error('This is demo feature, you must upload one json file.');
        return;
      }
      const file = fileList.item(0) as File;
      const fileReader = new FileReader();

      fileReader.readAsText(file, 'UTF-8');
      fileReader.onload = () => {
        console.log(fileReader.result);
      };
    });
    input.click();

  

posted @ 2020-12-07 19:31  JasonWangTing  阅读(1008)  评论(0编辑  收藏  举报