vue文件上传+pdf缩略图及预览

10 篇文章 0 订阅
1 篇文章 0 订阅
   <el-upload
                class="upload-demo"
                drag
                action="doUpload"
                :limit="1"
                :auto-upload="true"
                ref="upload"
                accept="application/pdf"
                :before-upload="beforeUpload"
              >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">上传文件</div>
                <!-- <div class="el-upload__text">将文件拖到此处,或<em>点击上传</em></div> -->
              </el-upload>

    <object
                  :data="fileData"
                  type="application/pdf"
                  width="100%"
                  height="100%"
                  internalinstanceid="5"
                ></object>

     <el-button
                  class="view"
                  round
                  size="small"
                  @click="viewFile()"
                >预览文件</el-button>
//上传文件之前的钩子
   beforeUpload(file) {
      console.log("文件", file);
      this.file = file;
      this.fileName = file.name;
      this.fileSize = file.size;
      let reader = new FileReader();
      reader.readAsDataURL(file);
      let that = this;
      reader.onload = function() {
        that.fileData = reader.result;
        // console.log("fileData", reader.result);
        console.log("fileData", that.fileData);
      };
      return false; // 返回false不会自动上传
    },


//预览文件
   viewFile() {
      console.log("viewFile");
      var win = window.open();
      win.document.write(
        '<body style="margin:0px;"><object data="' +
          this.fileData +
          '" type="application/pdf" width="100%" height="100%"><iframe src="' +
          this.fileData +
          '" scrolling="no" width="100%" height="100%" frameborder="0" ></iframe></object></body>'
      );
      // win.document.write(
      //   '<body style="margin:0px;"><iframe src="' +
      //     this.fileData +
      //     '" scrolling="no" width="100%" height="100%" frameborder="0" ></iframe></body>'
      // );
    },


//上传文件
    uploadContract() {
      let fileFormData = new FormData();
      fileFormData.append("file", this.file);
      fileFormData.append("doc_title", this.fileName);
      uploadContract(fileFormData).then(res => {
        if (res.code == 200) {
          console.log("uploadContract", res);
        } else {
          this.$message({
            message: res.msg,
            type: "warning"
          });
        }
      });
    },
//上传Formdata接口请求封装
export const HttpPostFormdata = (apiUrl, url, query) => {
  console.log("token", localStorage.getItem("token"));
  query = query || {};
  return instance
    .post(apiUrl + url + "?token=" + localStorage.getItem("token"), query, {
      headers: {
        "Content-Type": "multipart/form-data",
        token: localStorage.getItem("token")
      }
    })
    .then(res => {
      if (
        res.data.code == 1001 ||
        res.data.code == 1000 ||
        res.data.code == 1002
      ) {
        // eslint-disable-next-line no-console
        console.log("code1001");
        localStorage.token = "";
        localStorage.name = "";
        router.push({
          path: "/userLogin"
        });

        return res.data;
      } else {
        return res.data;
      }
    })
    .catch(() => {
      var dada = {
        msg: "系统错误!"
      };
      return dada;
    });
};
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值