最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

vue之el-upload使用FormData多文件同時上傳問題

 更新時間:2023年05月22日 15:00:30   作者:maidu_xbd  
這篇文章主要介紹了vue之el-upload使用FormData多文件同時上傳問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教

需求描述

使用el-upload 手動上傳方式進(jìn)行文件上傳【https://element.eleme.cn/#/zh-CN/component/upload】,當(dāng)選擇上傳多個文件時,選擇幾個文件就會向后臺發(fā)送幾次請求。

先后臺要求同時一次請求發(fā)送多個文件,包括文件(如圖中的file)和其他參數(shù)(如圖中的graphName和userID)

解決方法

通過FormData對象存放上傳的文件和參數(shù),將fileList中的文件存放在FormData中。具體見(3)多文件通過FormData存放上傳

(1)補(bǔ)充知識點(diǎn):FormData

FormData 數(shù)據(jù)形式為鍵值對,數(shù)據(jù)可通過XMLHttpRequest.send()方式發(fā)送出去

  • FormData.append(key,value):向FormData對象中添加一個鍵值對,如執(zhí)行FormData.append(key1,value1)后FormData.append(key1,value2),key1對應(yīng)值value1不會被覆蓋,而是新增對應(yīng)值value2
  • FormData.get(key):返回FormData對象中給定key對應(yīng)的第一個值
  • FormData.getAll(key):返回FormData對象中給定key對應(yīng)的所有值

FormData 具體使用見https://developer.mozilla.org/zh-CN/docs/Web/API/FormData/Using_FormData_Objects

(2)單文件手動上傳

  • :auto-upload="false"----關(guān)閉自動上傳
  • :limit="1"----限制上傳文件數(shù)量為1個
  •  :data="uploadData"----上傳文件時的附帶參數(shù)如userID
  • :action="batchImportUrl"----請求接口路徑

具體見代碼:

<el-dialog
      :visible.sync="batchImportDialogVisible"
      class="uploadFormDialog"
      width="40%"
      @close="closebatchImportForm"
    >
      <div slot="title">
        <span>{{ dialogTitle }}</span>
      </div>
      <el-upload
        class="upload-demo"
        ref="batchImport"
        :auto-upload="false"
        :on-error="batchImportError"
        :on-remove="batchRemoveFile"
        :before-upload="beforebatchImport"
        :on-progress="batchImportProgress"
        :on-success="batchImportSuccess"
        :on-change="batchImportChange"
        :file-list="fileList"
        :limit="1"
        :data="uploadData"
        :action="batchImportUrl"
      >
        <el-button
          slot="trigger"
          size="small"
          type="warning"
          @click="selectFile"
          >選取文件</el-button
        >
        <el-button size="small" type="success" @click="submitBatchImport">
          <span v-show="!isUploadFlag">上傳到服務(wù)器</span>
          <span v-show="isUploadFlag">
            正在上傳中
            <i class="el-icon-loading"></i>
          </span>
        </el-button>
        <div slot="tip" class="el-upload__tip">
          上傳文件格式為json或rdf,點(diǎn)擊
          <a class="download" @click="downloadTemplate">下載模板</a
          >&nbsp;&nbsp;可查看模板。
        </div>
      </el-upload>
    </el-dialog>
  // 批量導(dǎo)入新的圖譜信息
    batchAddGraph() {
      this.dialogTitle = "批量創(chuàng)建新圖譜";
      this.batchImportDialogVisible = true;
      this.batchImportUrl = " /api/manage/common/graph/add/batch";
    },
    // 批量導(dǎo)入已存在的圖譜信息
    batchUpdateGraph(name) {
      this.dialogTitle = `批量導(dǎo)入${name}圖譜`;
      this.uploadData = {
        userID: "",
        graphName: this.graphName
      };
      this.batchImportDialogVisible = true;
      this.batchImportUrl = " /api/manage/common/graph/update/increment";
    },
    // 提交批量導(dǎo)入
    submitBatchImport() {
      if (this.$refs.batchImport.uploadFiles.length == 0) {
        this.$message.warning("請選擇要上傳的文件");
        return;
      }
      this.loading = this.$loading({
        lock: true,
        text: "正在導(dǎo)入圖譜,請稍等",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.6)"
      });
      this.isUploadFlag = true;
      this.$refs.batchImport.submit();
    },
    // 選擇文件
    selectFile() {
      this.$refs.batchImport.clearFiles();
    },
    // 關(guān)閉對話框-清除照片
    closebatchImportForm() {
      this.isUploadFlag = false;
      this.$refs.batchImport.clearFiles();
    },
    // 批量上傳成功的鉤子
    batchImportSuccess(res, file, fileList) {
      if (res.respCode === "200") {
        this.$message.success("批量導(dǎo)入成功");
        this.graphDialogVisible = false;
        this.isUploadFlag = false;
        this.getGraphInfo();
      } else {
        this.$message.error(res.respDesc);
      }
      this.batchImportDialogVisible = false;
      this.loading.close();
    },
    // 批量導(dǎo)入失敗時的鉤子
    batchImportError() {
      this.$message.error(" 批量導(dǎo)入失敗");
      this.isUploadFlag = false;
      this.loading.close();
    },
    // 批量導(dǎo)入-文件狀態(tài)改變時的鉤子
    batchImportChange(file, fileList) {
      // console.log("文件狀態(tài)改變時的鉤子");
      if (!/.(json|rdf)$/.test(file.name)) {
        this.$refs.batchImport.uploadFiles = [];
        this.$message.warning("請選擇json或rdf格式的文件");
      }
    },
    // 上傳文件前的鉤子
    beforebatchImport(file) {
      // console.log("上傳文件前的鉤子");
    },
    // 文件上傳時的鉤子
    batchImportProgress(event, file, fileList) {
      // console.log("==文件上傳時progress==", file);
    },
    //文件列表移除文件時的鉤子
    batchRemoveFile() {
      // console.log("移除");
    }

(3)多文件通過FormData存放上傳

:file-list="fileList" 配置一個數(shù)組用于接收上傳的文件列表

multiple 選擇文件時允許多選

具體代碼:

<el-dialog
      :visible.sync="batchImportDialogVisible"
      class="uploadFormDialog"
      width="40%"
      @close="closebatchImportForm"
      :close-on-click-modal="false"
    >
      <div slot="title">
        <span>{{ dialogTitle }}</span>
      </div>
      <div v-if="dialogTitle == '批量創(chuàng)建新圖譜'" class="input-wrapper">
        <div class="name">圖譜名稱</div>
        <el-input v-model="bacthImportGraphName" clearable></el-input>
      </div>
      <el-upload
        class="upload-demo"
        ref="batchImport"
        :auto-upload="false"
        accept=".json,.csv,.rdf"
        :on-remove="batchRemoveFile"
        :on-change="batchImportChange"
        :on-exceed="batchImportExceed"
        :file-list="fileList"
        :limit="2"
        :action="batchImportUrl"
        multiple
      >
        <el-button
          slot="trigger"
          size="small"
          type="warning"
          @click="selectFile"
          >選取文件</el-button
        >
        <el-button size="small" type="success" @click="submitBatchImport">
          <span v-show="!isUploadFlag">上傳到服務(wù)器</span>
          <span v-show="isUploadFlag">
            正在上傳中
            <i class="el-icon-loading"></i>
          </span>
        </el-button>
        <div slot="tip" class="el-upload__tip">
          上傳文件格式為json、rdf或csv,點(diǎn)擊
          <a class="download" @click="downloadTemplate">下載模板</a
          >&nbsp;&nbsp;可查看模板。
        </div>
      </el-upload>
    </el-dialog>
// 選擇文件
    selectFile() {
      // this.$refs.batchImport.clearFiles();
    },
    // 批量導(dǎo)入新的圖譜信息
    batchAddGraph() {
      this.dialogTitle = "批量創(chuàng)建新圖譜";
      this.batchImportDialogVisible = true;
      this.uploadData.graphName = this.bacthImportGraphName; //綁定數(shù)據(jù)
      this.batchImportUrl = " /api/manage/common/graph/add/batch";
    },
    // 批量導(dǎo)入已存在的圖譜信息
    batchUpdateGraph(name) {
      this.dialogTitle = `批量導(dǎo)入${name}圖譜`;
      this.uploadData = {
        userID: "",
        graphName: this.graphName
      };
      this.batchImportDialogVisible = true;
      this.batchImportUrl = " /api/manage/common/graph/update/increment";
    },
    // 批量導(dǎo)入-文件狀態(tài)改變時的鉤子
    batchImportChange(file, fileList) {
      // console.log("文件狀態(tài)改變時的鉤子");
      this.fileList = fileList;
    },
    //文件列表移除文件時的鉤子
    batchRemoveFile(file, fileList) {
      // console.log("文件列表移除文件時的鉤子");
      this.fileList = fileList;
    },
    // 提交批量導(dǎo)入
    submitBatchImport() {
      if (this.dialogTitle == "批量創(chuàng)建新圖譜") {
        this.uploadData.graphName = this.bacthImportGraphName;
        // 1.提交批量創(chuàng)建新圖譜
        if (!this.bacthImportGraphName) {
          this.$message.warning("請?zhí)顚憟D譜名稱");
          return;
        }
      }
      if (this.$refs.batchImport.uploadFiles.length == 0) {
        this.$message.warning("請選擇要上傳的文件");
        return;
      }
      this.loading = this.$loading({
        lock: true,
        text: "正在導(dǎo)入圖譜,請稍等",
        spinner: "el-icon-loading",
        background: "rgba(0, 0, 0, 0.6)"
      });
      this.isUploadFlag = true;
      // this.$refs.batchImport.submit();
      let formData = new FormData(); //  用FormData存放上傳文件
      this.fileList.forEach(file => {
        formData.append("file", file.raw); //文件
      });
      formData.append("graphName", this.uploadData.graphName);
      formData.append("userID", "13013");
      axios
        .post(this.batchImportUrl, formData, {
          headers: { "Content-Type": "multipart/form-data" } //設(shè)置請求頭請求格式為JSON
        })
        .then(res => {
          this.$message.success(res.data.respDesc);
          this.graphDialogVisible = false;
          this.isUploadFlag = false;
          this.bacthImportGraphName = "";
          this.getGraphInfo();
          this.loading.close();
          this.batchImportDialogVisible = false;
        })
        .catch(err => {
          console.log(err);
        });
    },
    // 關(guān)閉對話框-清除照片
    closebatchImportForm() {
      this.isUploadFlag = false;
      this.$refs.batchImport.clearFiles();
    },
    // 批量導(dǎo)入-定義超出限制時的行為
    batchImportExceed(files, fileList) {
      this.$message.warning(
        `當(dāng)前限制選擇 2 個文件,本次選擇了 ${
          files.length
        } 個文件,共選擇了 ${files.length + fileList.length} 個文件`
      );
    },

總結(jié)

以上為個人經(jīng)驗(yàn),希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • Vue中的組件詳談

    Vue中的組件詳談

    這篇文章主要介紹了Vue的組件,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下
    2021-10-10
  • 詳解Vue.js自定義tipOnce指令用法實(shí)例

    詳解Vue.js自定義tipOnce指令用法實(shí)例

    這篇文章主要介紹了詳解Vue.js自定義tipOnce指令用法實(shí)例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-12-12
  • Vue Spa切換頁面時更改標(biāo)題的實(shí)例代碼

    Vue Spa切換頁面時更改標(biāo)題的實(shí)例代碼

    本篇文章主要介紹了Vue Spa切換頁面時更改標(biāo)題的實(shí)例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-07-07
  • Vue項(xiàng)目如何根據(jù)圖片url獲取file對象并用axios上傳

    Vue項(xiàng)目如何根據(jù)圖片url獲取file對象并用axios上傳

    這篇文章主要介紹了Vue項(xiàng)目如何根據(jù)圖片url獲取file對象并用axios上傳問題,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-09-09
  • vue引入Excel表格插件的方法

    vue引入Excel表格插件的方法

    這篇文章主要為大家詳細(xì)介紹了vue引入Excel表格插件的方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-04-04
  • 解決在Vue中使用axios用form表單出現(xiàn)的問題

    解決在Vue中使用axios用form表單出現(xiàn)的問題

    今天小編就為大家分享一篇解決在Vue中使用axios用form表單出現(xiàn)的問題,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2019-10-10
  • Vue CLI3中使用compass normalize的方法

    Vue CLI3中使用compass normalize的方法

    這篇文章主要介紹了Vue CLI3中使用compass normalize的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05
  • vuex直接賦值的三種方法總結(jié)

    vuex直接賦值的三種方法總結(jié)

    今天小編就為大家分享一篇vuex直接賦值的三種方法總結(jié),具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2018-09-09
  • 記錄vue做微信自定義分享的一些問題

    記錄vue做微信自定義分享的一些問題

    這篇文章主要介紹了記錄vue做微信自定義分享的一些問題,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-09-09
  • vue3+three.js的理解與簡單使用示例代碼

    vue3+three.js的理解與簡單使用示例代碼

    Three.js是一個基于WebGL的開源JavaScript?3D引擎,它簡化了Web?3D開發(fā)流程,讓開發(fā)者能夠輕松創(chuàng)建3D場景、動畫和交互體驗(yàn),這篇文章主要介紹了vue3+three.js理解與簡單使用的相關(guān)資料,需要的朋友可以參考下
    2026-02-02

最新評論

灵寿县| 朔州市| 方正县| 阳朔县| 得荣县| 黄平县| 阳高县| 璧山县| 任丘市| 云林县| 万安县| 株洲市| 印江| 仲巴县| 称多县| 卢龙县| 崇信县| 来宾市| 宜君县| 北海市| 满洲里市| 营口市| 南皮县| 辰溪县| 兴化市| 石嘴山市| 禹城市| 峨眉山市| 德江县| 巴林右旗| 重庆市| 慈溪市| 庆安县| 黄山市| 乃东县| 鄂尔多斯市| 龙江县| 松桃| 天津市| 西林县| 密云县|