上传txt文件

模版


              选取文件
              
{{ tips }}

data

fileList: [],
tips: "只能上传txt格式的文件",

methods

handleFileChange(file, fileList) {
        let that = this;
        that.beforeUpload(file);
        that.file= file;
        that.fileList = [fileList[fileList.length - 1]]; // 这一步,是 展示最后一次选择的dat文件
        if (that.fileList.length) {
        let params = new FormData();
        that.fileList.forEach(item => {
          params.append("MultipartFile", item.raw)
        })
        var config = {
          // 添加请求头
            headers: { 
              "Content-Type": "multipart/form-data;" 
            },
        };
        API.addDamageDegree(params, config).then(function (res) {
          if (res.code == "200") {
              that.isVisible = false
                that.$emit('success')
          } else {
            that.$message.error({
              showClose: true,
              message: res.data.message,
              duration: 5000,
            });
          }
        })
        .catch(function (error) {
          that.buttonLoading = false;
          that.$message.error({
            showClose: true,
            message: "请求出现异常",
            duration: 2000,
          });
        });
      }
      
    },
httpRequest(param) {
      let fileObj = param.file; // 相当于input里取得的files
      let fileName = fileObj.name;
      let formData = new FormData(); // FormData 对象
      formData.append("MultipartFile", fileObj); // 文件对象
      formData.append("fileCreateName", fileName);
      this.$emit("upload", formData);
    },

    isFormatValid(type) {
      let pStrDAt = /\.txt?$/i;
      return pStrDAt.test(type);
    },
    beforeUpload(file) {
      let fileName = file.name;
    //   this.$emit("update:fileName", fileName);
      let isDat = this.isFormatValid(fileName);
      if (!isDat) {
        this.tips = "当前选择的文件格式不正确,请重新选择!";
      } else {
        this.tips = "";
      }
      return isDat;
    },

你可能感兴趣的:(上传txt文件)