hejianhao
2025-01-09 2ce5f15695e9358655b0c1025cf9cc72d74a0c66
文件上传
1个文件已修改
90 ■■■■■ 已修改文件
src/view/addOrder.vue 90 ●●●●● 补丁 | 查看 | 原始文档 | blame | 历史
src/view/addOrder.vue
@@ -958,88 +958,62 @@
    },
    async toArrayBuffer(file) {
      return new Promise((resolve, reject) => {
        // 读取文件
        const fileReader = new FileReader();
        setTimeout(() => {
          fileReader.readAsArrayBuffer(file);
        });
        fileReader.readAsArrayBuffer(file);
        fileReader.onload = (e) => {
          resolve(e.target.result);
        };
        fileReader.onerror = (e) => {
          reject(e);
        };
        // 读取完成
        // fileReader.onload = (e) => {
        //   resolve(e.target?.result);
        // };
      });
    },
    async upFileAsArrayBuffer(
      url,
      file,
      options,
    ){
      // 读取文件
    async upFileAsArrayBuffer(url, file, options) {
      const arrayBuffer = await this.toArrayBuffer(file);
      // 上传文件
      return axios({
        url,
        method: options?.method || 'POST',
        withCredentials: Boolean(options?.withCredentials),
        // 二进制数据
        method: options.method || 'POST',
        withCredentials: Boolean(options.withCredentials),
        data: arrayBuffer,
        // 请求头
        headers: {
          'Content-Type': 'application/octet-stream',
          'file-name': encodeURIComponent(options?.fileName || file.name),
          'file-name': encodeURIComponent(options.fileName || file.name),
        },
        // 上传进度
        onUploadProgress: ({ total, loaded }) => {
          const onProgress = options?.onProgress;
          if (!isFunction(onProgress)) return;
          onProgress({
            percent: Math.ceil((loaded / total) * 100),
          });
          const onProgress = options.onProgress;
          if (typeof onProgress === 'function') {
            onProgress({
              percent: Math.ceil((loaded / total) * 100),
            });
          }
        },
      });
    },
    uploadFile(e, obj, value) {
      axios.post('http://47.108.239.173:2020/api/oss/new_temp_upload', obj).then(res => {
        if (res.status == 200) {
          this.upFileAsArrayBuffer('http://47.108.239.173:2020/api/oss/upload_file', e.file, { methods: 'POST', fileName: res.data.data }).then(resp => {
            console.log(resp, 'asdasdsad');
            if (resp.data.status === 0) {
              if (value == 1) {
                this.form.DOFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
                console.log(this.fileList1, 'this.fileList1');
          this.upFileAsArrayBuffer('http://47.108.239.173:2020/api/oss/upload_file', e.file).then(resp=>{
            console.log(resp,'asdasdsad');
              } else if (value == 2) {
                this.form.PUFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
                console.log(this.form.PUFileUrl, 'this.form.PUFileUrl');
              } else if (value == 3) {
                this.form.PODFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
              } else {
                this.form.otherFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
              }
              this.$forceUpdate()
            }
          })
          // axios.post('http://47.108.239.173:2020/api/oss/upload_file', fileName, {
          //   headers: {
          //     'File-Name': res.data.data
          //   }
          // }).then(resp => {
          //   console.log(resp, value, 'asdasdasdasdas');
          //   if (resp.data.status === 0) {
          //     if (value == 1) {
          //       this.form.DOFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
          //       console.log(this.fileList1, 'this.fileList1');
          //     } else if (value == 2) {
          //       this.form.PUFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
          //       console.log(this.form.PUFileUrl, 'this.form.PUFileUrl');
          //     } else if (value == 3) {
          //       this.form.PODFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
          //     } else {
          //       this.form.otherFileUrl = 'http://47.108.239.173:2020/api/oss/download/' + res.data.data
          //     }
          //     this.$forceUpdate()
          //   }
          // })
        }
      })
    },