Vue2+element文件上传预览的功能实现

<template>
  <el-dialog
    title="uploadFile"
    :visible.sync="dialogVisible"
    width="60%"
    :append-to-body="true"
    :modal-append-to-body="false"
    :before-close="closeDialog"
  >
    <div class="upload-file-box">
      <el-upload
        :action="fileUrl"
        :headers="headers"
        :before-upload="beforeUpload"
        :on-preview="handlePreview"
        :on-remove="handleRemove"
        :on-success="handleSuccess"
        :before-remove="beforeRemove"
        multiple
        :limit="5"
        :on-exceed="handleExceed"
        :file-list="fileList"
      >
        <el-button size="small" type="primary" icon="el-icon-plus"
          >上传文件</el-button
        >
        <!-- <div slot="tip" class="el-upload__tip">可上传图片、Excel、Word、pdf格式文件</div> -->
      </el-upload>
    </div>

    <!-- 附件预览的弹窗 -->
    <div>
      <el-dialog
        title="附件预览"
        :visible.sync="dialogVisible1"
        width="70%"
        :before-close="closeDialog1"
        :modal-append-to-body="false"
        :append-to-body="true"
      >
        <iframe style="width: 100%;height: 600px" :src="pathFile"></iframe>
        <div slot="footer" class="dialog-footer">
          <el-button
            class="filter_button"
            type="primary"
            @click="dialogVisible1 = false"
            >关 闭</el-button
          >
        </div>
      </el-dialog>
    </div>

    <span slot="footer" class="dialog-footer">
      <el-button @click="closeDialog">关 闭</el-button>
    </span>
  </el-dialog>
</template>


<style lang="scss" scoped>
.upload-file-box {
  height: 380px;
  overflow: auto;
}
</style>
data() {
    return {
      dialogVisible: false,
      dialogVisible1: false,
      fileUrl: "文件上传的接口",
      headers: { '自定义名称': "请求头" }, // 例如: headers: { Authorization: "adsda" },
      pathFile: "",
      fileUrlLoad: "文件回显的接口",
      fileList: [],
      formData: {
        enclosureFiles: []
      }
    };
  },
  methods: {
    showDialog() {
      this.dialogVisible = true;
    },
    closeDialog() {
      this.dialogVisible = false;
    },
    closeDialog1() {
      this.dialogVisible1 = false;
    },

    /* 文件上传 */
    beforeUpload(file) {},
    handlePreview(file) {
      console.log(file);
      this.dialogVisible1 = true;
      if (file.url) {
        this.pathFile = this.fileUrlLoad + file.url;
      }
      if (file.path) {
        this.pathFile = this.fileUrlLoad + file.path;
      } else {
        this.pathFile = this.fileUrlLoad + file.response.data.path;
      }
    }, // 关键是这一步,把回显的地址给pathFile不同的后端返回的也不同,需自行修改
    handleRemove(file, fileList) {
      const that = this;
      this.formData.enclosureFiles.forEach((item, index) => {
        if (file.response) {
          if (item.id === file.response.data.id) {
            this.formData.enclosureFiles.splice(index, 1);
          }
        } else {
          if (item.id === file.id) {
            this.formData.enclosureFiles.splice(index, 1);
          }
        }
      });
    },
    handleSuccess(response, file, fileList) {
      // console.log(response);
      this.formData.enclosureFiles.push({
        uid: file.uid,
        id: response.data.id
      });
    },
    beforeRemove(file, fileList) {
      // return this.$confirm(`确定移除 ${file.name}?`);
    },
    handleExceed(files, fileList) {
      this.$message.warning(
        `当前限制选择 5 个文件,本次选择了 ${
          files.length
        } 个文件,共选择了 ${files.length + fileList.length} 个文件`
      );
    }
  }

图片预览效果

附件预览效果

  • 2
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Vue Element文件上传,可以使用ElUpload组件。在模板中添加一个el-upload标签,并设置相关属性,如ref、:file-list、:auto-upload、:on-change、:limit、drag、action和accept。其中,ref用于在代码中获取上传组件的实例,:file-list绑定文件列表,:auto-upload设置为false表示手动触发上传,:on-change绑定一个方法来处理文件改变事件,:limit设置最大可选文件数量,drag表示是否启用拖拽上传,action为后台接口的URL,accept用于指定接受上传的文件类型。 在methods中,可以定义一些方法来处理文件的操作。例如,handleRemove用于移除文件,handlePreview用于预览文件,handleExceed用于处理超出文件数量限制的情况,beforeRemove用于确认是否移除文件。另外,可以定义submit方法来处理文件上传操作。在该方法中,可以创建一个FormData对象,将文件列表中的文件添加到FormData中,然后调用上传文件的API发送请求。在文件状态改变时的钩子函数OnChange中,可以更新文件列表。 示例代码如下: <el-upload class="upload-demo" ref="upload" :file-list="fileEditList" :auto-upload="false" :on-change="uploadEditFile" :limit="1" drag action accept=".kmz"> <i class="el-icon-upload"></i> <div class="el-upload__text">将文件拖到此处,或点击上传</div> <div class="el-upload__tip" slot="tip" style="display: table-cell; color: gray">只能上传.kmz文件</div> </el-upload> 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}?`); }, submit() { var newFile = new FormData(); this.fileList.forEach((item, index) => { newFile.append("file", item.raw); }) uploadFilePort(newFile).then( (res) => { console.log("文件上传res", res); if (res.data.code == 200) { this.$message({ message: "添加成功!!", type: "success" }); } }, (err) => { this.$message.error(err); } ); }, OnChange(file, fileList) { console.log("OnChange-fileList", file, fileList); this.fileList = fileList; }, },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值