zoukankan      html  css  js  c++  java
  • 解决excel文件上传时更改选中的文件出现错误net::ERR_UPLOAD_FILE_CHANGED

    原因:文件修改内容后file的本地文件已经丢失

    解决:
    1.上传beforeUpload时将file转成base64(bese64与本地的文件状态已经无关了),再转成file。(这是比较麻烦的,可以先问下后台能不能改,通过blob或者arraybuffer获取数据)

    保存base64格式

      const reader1 = new FileReader();
      reader1.readAsDataURL(file);
      reader1.onload = e => {
        this.base64Excel = e.target.result;
      };
    

    base64转file方法:

      base64ConvertFile (urlData, filename) { // 64转file
        var arr = urlData.split(',');
        var type = arr[0].match(/:(.*?);/)[1];
        var fileExt = type.split('/')[1];
        var bstr = atob(arr[1]);
        var n = bstr.length;
        var u8arr = new Uint8Array(n);
        while (n--) {
          u8arr[n] = bstr.charCodeAt(n);
        }
        return new File([ u8arr ], filename, {
          type: type
        });
      },
    

    最后把转出来的file通过formdata上传

      let excel = new FormData();
      let form = this.base64ConvertFile(this.base64Excel, this.excelFile.name);
      excel.append('file', form);
    

    2.点击上传时给出文件修改的提示

      this.file.slice( 0, 1 ) // only the first byte
      .arrayBuffer() // try to read
      .then( () => {
        // 文件没改变,在这里可以发请求了
        console.log( 'should be fine' );
        axios({.........})
      } )
      .catch( (err) => {
        // 文件有问题,在这里终止掉
        console.log( 'failed to read' );
        this.file = null; // 把缓存的file清空
      } );
    
  • 相关阅读:
    《A First Course in Probability》-chaper5-连续型随机变量-随机变量函数的期望
    Codeforces 837F
    Codeforces #428 Div2 D
    poj3233(等比矩阵求和)
    Codeforces #427 Div2 D
    Codeforces 837E
    hdu6086(AC 自动机)
    hdu2825(AC 自动机)
    poj2778(AC 自动机)
    Codeforces #426 Div2 D(线段树优化 DP )
  • 原文地址:https://www.cnblogs.com/yibottlec/p/15405758.html
Copyright © 2011-2022 走看看