用vue+el-upload+new FormData()实现多图片上传

用vue+el-upload+new FormData()实现多图片上传
如图
在这里插入图片描述

   <div class="relicsImage_content">
      <el-upload
        class="upload_reilcimg"
        ref="uploadImg"
        :action="baseurl + '/antique/batch_img'"
        list-type="picture-card"
        :headers="headers"
        :before-upload="beforeAvatarUploadMore"
        :on-success="handleAvatarSuccess"
        :on-remove="handleRemove"
        :on-change="changeUpload"
        :show-file-list="true"
        accept="image/png, image/jpeg, application/pdf"
        :multiple="true"
        :auto-upload="false"
      >
        <i class="el-icon-plus" />
      </el-upload>
      <p style="font-size: 12px; color: #ccc; margin-top: 20px">
        可上传pdf、jpg、jpeg、png格式,不能超过50M
      </p>
    </div>
    <div>
      <el-button type="primary" @click="uploadSumit()">确定</el-button>
      <el-button @click="reset()">重置</el-button>
    </div>
  data() {
    return {
      ssd: [],
      isActive: false,
      forma: {
        copyReportCode: "",
      },
      list: [],
      page: 1,
      limit: 5, //
      total: 0,
      id: "", //代表选中某一行的id
      multipleSelection: [],
      uploadMoreimg: {
        fileList: [],
      },
      url: this.baseurl + "/antique_levy/upload",
      headers: {
        Authorization: sessionStorage.getItem("token"),
      },
    };
  },
  created() {},
  methods: {
    reset() {
      this.$refs.uploadImg.clearFiles();
    },
    uploadSumit() {
      // 创建新的数据对象
      let formData = new FormData();
      // 将上传的文件放到数据对象中
      this.uploadMoreimg.fileList.forEach((file) => {
        formData.append("file", file.raw);
      });
      console.log("提交前", formData.getAll("file"));
      // 自定义上传
      this.axios
        .post("/antique/batch_img", formData)
        .then((response) => {
          console.log(response);
          this.isshowMoreimg = false;
          // if(response.code == 200){
          //   this.$refs.uploadImg.clearFiles();
          //   this.msgSuccess('上传成功!');
          // }else{
          //   this.$message.error(response.msg);
          // }
        })
        .catch((error) => {
          this.$message.error("上传失败!");
        });
    },
    openDialog(employeeId) {
      this.isshowMoreimg = true;
    },
    downLoad(row) {
      this.downloadByA({
        url: this.baseurl + "/collection_copy_report/print?id=" + row.id,
        name: "藏品编目",
      });
    },
    downloadByA({ url, name = "temp" }) {
      // 生成一个a元素
      const a = document.createElement("a");
      // 创建一个单击事件
      const event = new MouseEvent("click");
      // 设置图片名称
      a.download = name;
      // 如果跳转页面,则在其他页面跳转
      a.target = "_blank";
      // 将生成的URL设置为a.href属性
      a.href = url;
      // 触发a的单击事件
      a.dispatchEvent(event);
    },

    // 上传之前
    beforeAvatarUploadMore(file) {
      const isJPG = file.type === "image/jpeg";
      const isPng = file.type === "image/png";
      const isPdf = file.type === "application/pdf";
      //   const isLt50M = file.size / 1024 / 1024 < 50;
      //   if (!isLt50M) {
      //     this.$message.warning("上传文件大小不能超过 50MB!");
      //     return false;
      //   }
      //   if (!isJPG && !isPng && !isPdf) {
      //     this.$message.warning("只能上传 JPG/PNG/PDF/JPEG 格式!");
      //     return false;
      //   }
    },
    // 上传成功
    handleAvatarSuccess() {},
    // 删除
    handleRemove(file, fileList) {
      this.uploadMoreimg.fileList = fileList;
    },
    // 文件状态改变时的钩子,添加文件、上传成功和上传失败时都会被调用
    changeUpload(file, fileList) {
      this.uploadMoreimg.fileList = fileList;
    },
  },
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现Vue + Axios + El-upload进行文件上传并跨域,需要按照以下步骤进行操作: 1. 在Vue项目中安装axios和element-ui。在命令行中输入以下命令即可安装: ``` npm install axios element-ui --save ``` 2. 在Vue项目中创建一个上传文件的组件,例如FileUpload.vue。在该组件中导入axios和element-ui,并且引入El-upload组件。示例代码如下: ```vue <template> <div> <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleUploadSuccess" :before-upload="beforeUpload" :headers="headers" :data="formData" :file-list="fileList"> <el-button size="small" type="primary">点击上传</el-button> </el-upload> </div> </template> <script> import axios from 'axios' import { Message } from 'element-ui' export default { name: 'FileUpload', data () { return { fileList: [], uploadUrl: 'http://example.com/upload', formData: {}, headers: { 'Content-Type': 'multipart/form-data' } } }, methods: { handleUploadSuccess (response, file, fileList) { // 上传成功后的处理逻辑 console.log(response) }, beforeUpload (file) { // 文件上传前的处理逻辑 console.log(file) } } } </script> ``` 3. 在组件中实现文件上传的逻辑。在上传文件之前,需要设置请求头和请求数据,并且需要处理跨域请求。可以在组件的methods中定义一个upload方法,用来发送上传请求。示例代码如下: ```vue <script> import axios from 'axios' import { Message } from 'element-ui' export default { name: 'FileUpload', data () { return { fileList: [], uploadUrl: 'http://example.com/upload', formData: {}, headers: { 'Content-Type': 'multipart/form-data' } } }, methods: { handleUploadSuccess (response, file, fileList) { // 上传成功后的处理逻辑 console.log(response) }, beforeUpload (file) { // 文件上传前的处理逻辑 console.log(file) }, upload () { let config = { headers: this.headers, withCredentials: true // 跨域请求需要设置withCredentials为true } let data = new FormData() data.append('file', this.fileList[0].raw) // 向后端发送上传请求 axios.post(this.uploadUrl, data, config) .then(response => { this.handleUploadSuccess(response) }) .catch(error => { console.log(error) }) } } } </script> ``` 4. 最后,在Vue组件中使用El-upload组件,并且调用upload方法即可实现文件上传。示例代码如下: ```vue <template> <div> <el-upload class="upload-demo" :action="uploadUrl" :on-success="handleUploadSuccess" :before-upload="beforeUpload" :headers="headers" :data="formData" :file-list="fileList"> <el-button size="small" type="primary" @click="upload">点击上传</el-button> </el-upload> </div> </template> <script> import axios from 'axios' import { Message } from 'element-ui' export default { name: 'FileUpload', data () { return { fileList: [], uploadUrl: 'http://example.com/upload', formData: {}, headers: { 'Content-Type': 'multipart/form-data' } } }, methods: { handleUploadSuccess (response, file, fileList) { // 上传成功后的处理逻辑 console.log(response) }, beforeUpload (file) { // 文件上传前的处理逻辑 console.log(file) }, upload () { let config = { headers: this.headers, withCredentials: true // 跨域请求需要设置withCredentials为true } let data = new FormData() data.append('file', this.fileList[0].raw) // 向后端发送上传请求 axios.post(this.uploadUrl, data, config) .then(response => { this.handleUploadSuccess(response) }) .catch(error => { console.log(error) }) } } } </script> ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值