首页 > 其他分享 >el-upload以及blob自动根据列表名称匹配下载

el-upload以及blob自动根据列表名称匹配下载

时间:2024-05-07 14:35:20浏览次数:21  
标签:el name upload typeSet application blob file else type

dom文档

<el-row :gutter="20">
            <el-col :span="16" :offset="0">
              <el-form-item label-width="120px" label="文件上传">
                <el-upload
                  ref="upload"
                  class="upload-file-uploader upload-demo"
                  action=""
                  :http-request="UploadImage"
                  :multiple="false"
                  :file-list="fileList"
                  :before-upload="handleBeforeUpload"
                  :on-success="successFileFolder"
                  :on-error="uploadError"
                  :show-file-list="true"
                  :accept="accept"
                >
                  <el-button class="left" size="small" type="primary"
                    >选择上传文件</el-button
                  >
                  <div slot="file" slot-scope="{ file }">
                    <li class="el-upload-list__item is-success">
                      <a
                        @click="() => openUrl(file)"
                        class="el-upload-list__item-name"
                      >
                        <i class="el-icon-document"></i>{{ file.name }}
                      </a>
                      <label class="el-upload-list__item-status-label">
                        <i
                          class="el-icon-upload-success el-icon-circle-check"
                        ></i>
                      </label>
                      <i
                        @click.stop="handleRemove(file)"
                        class="el-icon-close"
                      ></i>
                    </li>
                  </div>
                </el-upload>
              </el-form-item>
            </el-col>
          </el-row>

fileList内容

 if (rows) {
        for (let i = 0; i < rows.length; i++) {
          file.push({
           name: rows[i].taskFileName,
            id: rows[i].id,
          });
         }
  }
 this.fileList = file;

剩下几个对于的函数

UploadImage(param) {
      const formData = new FormData();
      var data = formData;
      if (this.taskNo != "") {
        formData.append("taskNo", this.taskNo);
      }
      formData.append("file", param.file); // 要提交给后台的文件,并且字段的key为Filedata
      request({
        url: "/task/file/upload",
        headers: { "Content-Type": "multipart/form-data" },
        data: data,
        method: "post",
        onUploadProgress: (progressEvent) => {
          const complete = parseInt(
            ((progressEvent.loaded / progressEvent.total) * 100) | 0,
            10
          );
          param.onProgress({ percent: complete });
        },
      })
        .then((res) => {
          if (res.code == 200) {
            this.$message.success("上传成功");
            this.$refs.upload.clearFiles();
            this.fileList = [];
            this.taskNo = res.data.taskNo;
            this.getFileList();
          }
          return true;
        })
        .catch((err) => {
          this.$refs.upload.clearFiles();
          this.fileList = [];
        });
    },
// 上传前校检格式和大小
    handleBeforeUpload(file) {
      this.oldFileName = file.name;
      // 校检文件类型
      if (this.fileType) {
        let fileExtension = "";
        if (file.name.lastIndexOf(".") > -1) {
          fileExtension = file.name.slice(file.name.lastIndexOf(".") + 1);
        }
        const isTypeOk = this.fileType.some((type) => {
          if (file.type.indexOf(type) > -1) return true;
          if (fileExtension && fileExtension.indexOf(type) > -1) return true;
          return false;
        });
        if (!isTypeOk) {
          this.$message.error(
            `文件格式不正确, 请上传${this.fileType.join("/")}格式文件!`
          );
          return false;
        }
      }
      // 校检文件大小
      if (this.fileSize) {
        const isLt = file.size / 1024 / 1024 < this.fileSize;
        if (!isLt) {
          this.$message.error(`上传文件大小不能超过 ${this.fileSize} MB!`);
          return false;
        }
      }

      return true;
    },
/** 导出列表 */
    downLoad(data, name, type) {
      return request({
        url: "/task/file/download",
        method: "post",
        headers: {
          "Content-Type": "application/json;charset=UTF-8",
        },
        responseType: "blob",
        // responseType: 'arraybuffer',
        data: data,
      }).then((res) => {
        let typeSet = "application/vnd.ms-excel";
        if (type == ".xls") {
          typeSet = "application/vnd.ms-excel";
        } else if (type == ".xlsx") {
          typeSet =
            "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
        } else if (type == ".doc") {
          typeSet = "application/msword";
        } else if (type == ".docx") {
          typeSet =
            "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        } else if (type == ".png") {
          typeSet = "pplication/pdf";
        } else if (type == ".ppt") {
          typeSet = "application/vnd.ms-powerpoint";
        } else if (type == ".png") {
          typeSet = "image/png";
        } else if (type == ".pptx") {
          typeSet =
            "application/vnd.openxmlformats-officedocument.presentationml.presentation";
        } else if (type == ".jpeg") {
          typeSet = "image/jpeg";
        } else if (type == ".zip") {
          typeSet = "application/zip";
        } else if (type == ".7z") {
          typeSet = "application/x-7z-compressed";
        } else if (type == "tar") {
          typeSet = "application/x-tar";
        } else if (type == ".7z") {
          typeSet = "application/x-7z-compressed";
        }
        var url = window.URL.createObjectURL(
          new Blob([res], { type: typeSet })
        );
        const link = document.createElement("a");
        link.style.display = "none";
        link.href = url;
        link.setAttribute("download", name);
        document.body.appendChild(link);
        link.click();
        document.body.removeChild(link);
      });
    },
successFileFolder(re, file, fileList) {
      // fileList = fileList.filter((item) => item.response.code !== 500);
      // this.fileList = fileList;
    },
    uploadError() {
      this.$message.error("文件上传失败,请检查文件大小或文件格式");
    },
openUrl(file) {
      console.log(file);
      let params = {
        fileId: parseInt(file.id),
      };
      this.downLoad(params, file.name.split(",")[0], file.name.split(",")[1]);
    },
    handleRemove(file) {
      console.log(file);
      let params = {
        fileIds: [file.id],
      };
      delFile(params).then((res) => {
        if (res.code == 200) {
          this.getFileList();
        }
      });
    },

 

标签:el,name,upload,typeSet,application,blob,file,else,type
From: https://www.cnblogs.com/sweeeper/p/18177151

相关文章

  • 【爬虫】项目篇-使用selenium、requests爬取天猫“华为手机”的商品评价
    目录使用selenium使用requests使用seleniumfromselenium.webdriverimportChrome,ChromeOptionsfromselenium.webdriver.support.waitimportWebDriverWaitfromselenium.webdriver.common.byimportByfromselenium.webdriver.supportimportexpected_conditionsasE......
  • BeanShell使用场景和语法
    BeanShell有自己的语法,同时支持java语法;使用场景:1.数据库断言2.RAS加密、混合加密3.接口签名4.调用开发给的jar包、class文件、java文件5.csv数据驱动之后的断言6.跨线程组共享变量分类:1.前置处理器:BeanShell2.后置处理器:BeanShell3.定时器:BeanShell4.采......
  • dubbo-ActiveLimitFilter
    服务消费者端限流ActiveLimitFilter限制客户端对interface或method的并发客户端调用。限流方式从url中获取actives并发数量,actives大于0进行并发控制,actives小于等于0不控制。消费者端使用RpcStatus下的ConcurrentMap<String,ConcurrentMap<String,RpcStatus>>存储每个metho......
  • dubbo-AdaptiveLoadBalance
    AdaptiveLoadBalance使用AdaptiveLoadBalanceFilter初始化的数据。核心处理org.apache.dubbo.rpc.cluster.loadbalance.AdaptiveLoadBalance#selectByP2Corg.apache.dubbo.rpc.cluster.loadbalance.AdaptiveLoadBalance#chooseLowLoadInvokerorg.apache.dubbo.rpc.AdaptiveMet......
  • Selenium自动化测试——个人博客系统
    fromseleniumimportwebdriverfromselenium.common.exceptionsimportTimeoutExceptionfromselenium.webdriver.common.byimportByfromselenium.webdriver.common.keysimportKeysfromselenium.webdriver.support.uiimportWebDriverWaitfromselenium.webdri......
  • 解决excel打开.csv文件乱码问题
    解决excel打开.csv文件乱码问题今天打开用Excel打开一个后缀为.csv的文件,出现乱码,然后用Emeditor转化文件格式为Utf-8,再用excel打开还是乱码,然后就有了以下步骤在Excel里面,点击数据=>从文本/CSV=>然后找到那个乱码.csv的文件,将其导入即可第二次我把制表符改成原始的逗号,......
  • Hello world! 记我的第一个随笔
    Hello,大家好啊,这是我在博客园写的第一个随笔,之前基本上都是在csdn和github上进行写东西的,最近突然想建立一个自己的博客,这样的话就可以写一点除了技术和经验以外的其他杂事了。把这个当是一个记事本吧,有当作一个交朋友的途径,维护好自己的小博客也是很有成就感的捏( ̄▽ ̄)*欢迎大家和......
  • 在英特尔至强 CPU 上使用 Optimum Intel 实现超快 SetFit 推理
    在缺少标注数据场景,SetFit是解决的建模问题的一个有前途的解决方案,其由HuggingFace与Intel实验室以及UKPLab合作共同开发。作为一个高效的框架,SetFit可用于对SentenceTransformers模型进行少样本微调。SetFit仅需很少的标注数据就能达到较高的准确率,例如,在使用3-......
  • [shell] fishshell -- 教程
    [shell] fishshell -- 教程   一、fishshell 官网  1.https://fishshell.com/     二、fishshell  文档 1.https://fishshell.com/docs/current/index.html     三、fishshell 教程1.h......
  • [转帖]Release Schedule of Current Database Releases (Doc ID 742060.1)
    https://support.oracle.com/knowledge/Oracle%20Database%20Products/742060_1.html APPLIESTO:OracleDatabase-StandardEdition-Version11.2.0.4andlaterOracleDatabaseCloudService-VersionN/AandlaterGen2ExadataCloudatCustomer-VersionAl......