vue+elementUI实现上传下载文件、图片

<!--添加/编辑-->
<template>
  <div class="edit_container">
    <el-form
      :model="userForm"
      :rules="userRules"
      ref="updatePage"
      label-width="120px"
      class="edit_form"
    >
      <!--附件地址-->
      <el-form-item :label="tableHeader.AttAdress" class="form_field" prop="AttAdress">
        <div style="display: flex">
          <el-input v-model.trim="userForm.AttAdress" disabled clearable ></el-input>
          <el-upload
            class="upload-demo"
            :action="getAction"
            :headers="headersObj"
            :on-preview="handlePreview"
            :on-remove="handleRemove"
            :on-success="handleAvatarSuccess"
            :before-remove="beforeRemove"
            multiple
            :limit="3"
            :on-exceed="handleExceed"
            :file-list="fileList"
            :show-file-list="false"
          >
            <el-button size="small" type="primary">点击上传</el-button>
          </el-upload>
        </div>
      </el-form-item>
    </el-form>

    <el-button @click="donwload">下载</el-button>
  </div>
</template>
<script>
import {FileDownload} from '@/api/WMSCommonApi'
export default {
  name: "portlet_update",
  props: {},
  data() {
    return {
      headersObj: { Authorization: this.$store.getters.token },
      fileList: [],
      getAction: process.env.VUE_APP_BASE_API_WMS + "aaa?path=default", //上传接口
    };
  },
  created() {},
  methods: {
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(file) {
      console.log(file);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 3 个文件,本次选择了 ${files.length} 个文件,共选择了 ${
          files.length + fileList.length
        } 个文件`
      );
    },
    beforeRemove(file, fileList) {
      return this.$confirm(`确定移除 ${file.name}`);
    },
    handleAvatarSuccess(val) {
      console.log(val);
      this.$message.success("上传成功");
      this.userForm.AttAdress = val.toString();
    },

    // 下载
    download(val) {
      var index = val.AttAdress.lastIndexOf("."); //获取.后边的字符串
      let obj = val.AttAdress.substring(index + 1, val.AttAdress.length); //文件类型不一样,截取要下载文件后缀
      FileDownload({ filePath: val.AttAdress }) .then((res) => {
          const content = res;
          const blob = new Blob([content]); // 接口返回的数据,依实例而行
          var fileName = "";
          fileName = val.AttName + "." + obj; // 文件名+后缀
          const elink = document.createElement("a");
          elink.download = fileName;
          elink.style.display = "none";
          elink.href = URL.createObjectURL(blob); // 文件流生成的url
          document.body.appendChild(elink);
          elink.click(); // 模拟在按钮上实现一次鼠标点击
          URL.revokeObjectURL(elink.href); // 释放URL 对象
          document.body.removeChild(elink); // 移除 a 标签
          this.btnName = this.commonBtnName.cmdDownloadTemplate;
          this.downloadLoading = false;
        })
        .catch(() => {
          this.btnName = this.commonBtnName.cmdDownloadTemplate;
          this.downloadLoading = false;
        });
    },
  },
};
</script>
<style lang="scss" scoped>
/deep/ .el-upload {
  height: 0;
  line-height: 0;
  button {
    padding: 6px 10px;
  }
}
</style>

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值