欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

JS實(shí)現(xiàn)Excel文件與圖片視頻上傳

 更新時(shí)間:2023年07月27日 10:41:32   作者:DCodes  
這篇文章主要為大家學(xué)習(xí)介紹了JavaScript如何實(shí)現(xiàn)Excel文件與圖片視頻上傳,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下

Excel上傳

excel的上傳其實(shí)分為兩步:

1、下載excel模板

2、上傳excel模板

在項(xiàng)目中涉及到excel的業(yè)務(wù),基本上都要先下載excel模板,用戶根據(jù)下載的模板填寫excel信息,然后將信息上傳到后臺(tái)。下面就這兩部分別說(shuō)明:

1、下載excel模板

關(guān)于下載excel模板的內(nèi)容請(qǐng)看:調(diào)用后臺(tái)接口實(shí)現(xiàn)Excel導(dǎo)出功能以及導(dǎo)出亂碼問(wèn)題解決(點(diǎn)擊直達(dá))

2、上傳excel模板

這里用到element 的上傳組件,element上傳組件提供點(diǎn)擊上傳和拖動(dòng)文件上傳。

<template>
  <div>
    <el-upload
      class="upload-demo"
      ref="upload"
      drag
      :data="uploadData"
      :action="actionUrl"
      :accept="acceptType"
      :headers="headers"
      :limit="fileLimit"
      :on-exceed="handleExceed"
      :file-list="fileList"
      :before-remove="beforeRemove"
      :on-progress="onProgress"
      :on-success="onSuccess"
      :on-error="onError"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
    </el-upload>
    <div v-if="fileInfo">
      <el-table :data="errorList" border style="width: 100%">
        <el-table-column prop="fileName" label="失敗文件" >
            </el-table-column>
        <el-table-column prop="error" label="失敗原因"> </el-table-column>
      </el-table>
    </div>
  </div>
</template>
<script>
import { getToken } from "@/utils/auth";
import { isArray } from "min-dash";
export default {
  name: "ExcelUpload",
  props: {
    uploadData: {
      type: Object,
      default: "",
    },
    actionUrl: {
      type: String,
      default: "",
    },
    acceptType: {
      type: String,
      default: "",
    },
  },
  components: {},
  data() {
    return {
      fileLimit: 1, // 最大值
      headers: {}, // 請(qǐng)求頭
      fileInfo: false, // 上傳文件
      errorList: [], // 錯(cuò)誤列表
      progress: {
        totalFileCount: 0, // 總文件
        handleFileCount: 0, // 上傳文件
      },
      fileList: [], // 上傳展示的文件列表,如果fileLimit = 1,該數(shù)組只能有一個(gè)成員
    };
  },
  created() {
    this.headers["Authorization"] = "Bearer " + getToken(); // 獲取請(qǐng)求頭信息
  },
  methods: {
    onSuccess(response, file, fileList) {
      if (response.code == 200) {
        if (Array.isArray(response.data)) {
          if (response.data.length > 0) {
            this.fileInfo = true;
            file.status = "error";
            this.errorList = [];
            this.errorList.push(...response.data);
            this.$message.error(`操作失敗`);
          } else {
            this.$emit("uploadBack");
          }
        } else {
          this.$emit("uploadBack");
        }
      } else {
        this.errorList = [];
        this.errorList.push({
          fileName: '',
          error: response.msg
        });
        this.fileInfo = true;
        file.status = "error";
        this.$message.error(`${response.msg}`);
      }
    },
    onError(err, file, fileList) {
      file.status = "error";
      this.$message.error(`${err.msg}`);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`上傳文件數(shù)超出限制`);
    },
    onProgress(event, file, fileList) {},
    beforeRemove(file, fileList) {
      this.$confirm("確定移出嗎?", "提示", {
        confirmButtonText: "確定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.fileList = [];
          this.fileInfo = false;
          this.errorFile = false;
        })
        .catch(() => {
          fileList.push(file);
        });
    },
  },
  mounted() {},
  watch: {},
  computed: {},
  filters: {},
};
</script>

這里是將element的上傳組件進(jìn)行二次封裝,該組件的職責(zé)就是負(fù)責(zé)接受上傳的文件、上傳地址、上傳類型,做通用化處理,在任何需要上傳功能的頁(yè)面引入即可使用,下面將組件拆開來(lái)講:

    <el-upload
      class="upload-demo"
      ref="upload"
      drag
      :data="uploadData"
      :action="actionUrl"
      :accept="acceptType"
      :headers="headers"
      :limit="fileLimit"
      :on-exceed="handleExceed"
      :file-list="fileList"
      :before-remove="beforeRemove"
      :on-progress="onProgress"
      :on-success="onSuccess"
      :on-error="onError"
    >
      <i class="el-icon-upload"></i>
      <div class="el-upload__text">將文件拖到此處,或<em>點(diǎn)擊上傳</em></div>
    </el-upload>

:data :element的上傳組件允許我們?cè)谏蟼鞯臅r(shí)候攜帶上傳參數(shù),這里的 :data 就是上傳攜帶的參數(shù),該參數(shù)由父組件提供(主要業(yè)務(wù)),這樣就可以在多種不同情況下使用,避免參數(shù)固定化。

:action:請(qǐng)求地址,這個(gè)請(qǐng)求地址就是后臺(tái)的接口,但是這個(gè)接口不能直接使用,需要在接口前加 process.env.VUE_APP_BASE_API 來(lái)判斷當(dāng)前的運(yùn)行環(huán)境。

:acceptType: 接受文件的類型,例如這里上傳 .xlsx 文件,那么點(diǎn)擊上傳的時(shí)候,只會(huì)讀取系統(tǒng)里 .xlsx 的文件。該類型也是由父頁(yè)面提供,這樣可以區(qū)分多種文件上傳情況,該組件可以適配多種文件類型,例如:.jpg、.png、.xlsx、.mp4等。

:headers:請(qǐng)求頭,需要加上token才能成功上傳。

      :limit="fileLimit"
      :on-exceed="handleExceed"
      :file-list="fileList"
      :before-remove="beforeRemove"
      :on-progress="onProgress"
      :on-success="onSuccess"
      :on-error="onError"

這些就是組件的一些事件以及屬性,根據(jù)element文檔閱讀即可,這里帶一下:

limit:最大上傳文件數(shù)

on-exceed:上傳文件超出最大數(shù)時(shí)的鉤子,可以做錯(cuò)誤提示

file-list:上傳文件的數(shù)組

before-remove:上前的事件

on-progress: 上傳中的事件,該事件在上傳過(guò)程中會(huì)持續(xù)調(diào)用,可以用來(lái)做進(jìn)度條展示。

on-success:上傳成功的事件,當(dāng)接口返回200,觸發(fā)該事件。

on-error: 上傳失敗的事件,當(dāng)接口返回500,觸發(fā)該事件。

頁(yè)面中的使用

      <ExcelUploadList
        @uploadBack="uploadBack"
        :acceptType="acceptType"
        :actionUrl="actionUrl"
        :uploadData="uploadData"
      ></ExcelUploadList>
export default {
  props: {
    area: Object,
    allArea: Object,
  },
  data() {
    return {
      acceptType: ".zip", // 上傳文件類型
      uploadData: {}, // 上傳附加的參數(shù)信息
      // 文件上傳的接口
      actionUrl: `${process.env.VUE_APP_BASE_API}/data/loadExcel/importZip`, 
    };
  },
  methods: {
    uploadBack(response) {
      // 上傳成功后觸發(fā)事件
      this.$message.success(`操作成功`);
      // this.getList(); 刷新列表
    },
    onImport() {
      if (!this.area.id) {
        return this.$message.warning("請(qǐng)選擇地區(qū)");
      }
      this.uploadData = {
        fiProvinceCode: this.allArea.fiProvinceCode, // 省 
        fiCityCode: this.allArea.fiCityCode, // 市
        fiAreaCode: this.allArea.fiAreaCode, // 區(qū)
        fiSubdistrictId: this.allArea.fiSubdistrictId, // 街道
        fiCommunityId: this.allArea.fiCommunityId, // 社區(qū)
      };
    },
  },
  created() {},
  activated() {},
  components: {
    ExcelUpload,
  },
};

通過(guò)父組件將自身業(yè)務(wù)所需的參數(shù)傳入到上傳組件中,具體的上傳業(yè)務(wù)由組件完成,完成后將結(jié)果返回給父組件展示或者刷新展示列表。

圖片和視頻

上文中上傳excel的組件,其實(shí)已經(jīng)可以用作圖片和視頻上傳了,我們只需要傳入對(duì)應(yīng)的圖片或視頻接口 actionUrl ,然后將對(duì)應(yīng)的上傳類型 acceptType:'.mp4'提供給組件,在選擇文件時(shí)就只讀取.mp4的文件,然后將上傳所需的參數(shù) uploadData 提供給組件,那么圖片和視頻上傳就已經(jīng)完成了。

上傳圖片和視頻并不需要模板,直接上傳即可,所以會(huì)比較方便。其實(shí)上傳文件都是將文件轉(zhuǎn)成file文件或者formData,將文件傳給后端即可。

// file轉(zhuǎn)formData
function files(file){
   let formData = new FormData();
   formData.append("file", file); // file轉(zhuǎn)formData
   //formData參數(shù)傳給后端
   ossUpload(formData).then(res=>{
       //....
   })
}

如果你要在formData上追加參數(shù),只需要:

let formData = new FormData();
formData.append("file", file); // file轉(zhuǎn)formData
formData.append("params1", val1);
formData.append("params2", val2);
// .......
//formData參數(shù)傳給后端
ossUpload(formData).then(res=>{
    //....
})

上傳的錯(cuò)誤提醒以及邏輯處理

某些情況下,我們需要對(duì)上傳文件做邏輯處理,比如上傳失敗該如何處理、上傳成功如何處理、能上傳幾個(gè)文件、將上傳文件的列表做刪除、上傳中的進(jìn)度處理等,這些邏輯,組件都提供了對(duì)應(yīng)的鉤子,如下:

  created() {
    this.headers["Authorization"] = "Bearer " + getToken();
  },
  methods: {
    // 上傳成功的鉤子,某些情況下需要做邏輯處理
    // 上傳5個(gè)文件,成功3個(gè),失敗2個(gè),這個(gè)時(shí)候需要在這里做上傳失敗的文件展示
    onSuccess(response, file, fileList) {
      if (response.code == 200) {
          // 如果失敗列表為數(shù)組證明有上傳失敗
        if (Array.isArray(response.data)) {
            // 上傳失敗列表數(shù)大于0表示有失敗文件
          if (response.data.length > 0) {
            this.fileInfo = true;  // 打開錯(cuò)誤列表展示彈窗
            file.status = "error"; // 更改類型,關(guān)聯(lián)css的字體變紅
            this.errorList = []; 
            this.errorList.push(...response.data); // 錯(cuò)誤列表數(shù)據(jù)
            this.$message.error(`操作失敗`); 
          } else {
            // 如果錯(cuò)誤列表為空,則表示全部上傳成功
            this.$emit("uploadBack");
          }
        } else {
          // 全部上傳成功
          this.$emit("uploadBack");
        }
      } else {
         // 上傳失敗
        this.errorList = [];
         // 錯(cuò)誤列表數(shù)據(jù)
        this.errorList.push({
          fileName: '',
          error: response.msg
        });
        this.fileInfo = true; // 打開錯(cuò)誤列表展示彈窗
        file.status = "error"; // 更改類型,關(guān)聯(lián)css的字體變紅
        this.$message.error(`${response.msg}`);
      }
    },
    // 上傳失敗的鉤子
    onError(err, file, fileList) {
      file.status = "error"; // 更改類型,關(guān)聯(lián)css的字體變紅
      this.$message.error(`${err.msg}`);
    },
    // 上傳文件數(shù)超出限制的鉤子
    handleExceed(files, fileList) {
      this.$message.warning(`上傳文件數(shù)超出限制`);
    },
    // 進(jìn)度條鉤子
    onProgress(event, file, fileList) {},
    // 點(diǎn)擊移出上傳文件的鉤子
    beforeRemove(file, fileList) {
      this.$confirm("確定移出嗎?", "提示", {
        confirmButtonText: "確定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          // 重置上傳文件的列表,這里限制最大上傳為 1 ,所以移出就直接重置
          this.fileList = [];
          this.fileInfo = false; // 關(guān)閉上傳失敗的信息彈窗
          this.errorFile = false; // 關(guān)閉錯(cuò)誤信息彈窗
        })
        .catch(() => {
          fileList.push(file); // 取消則將文件重新添加到列表,不做任何更改。
        });
    },
  },

上傳失敗,在文件展示這里將字體變?yōu)榧t色

<style lang='scss' scoped>
::v-deep .el-upload-list__item.is-error {
  .el-upload-list__item-name {
    color: #ff4949 !important;
  }
}
</style>

上傳進(jìn)度處理

element上傳組件幫我們關(guān)聯(lián)了上傳進(jìn)度,從發(fā)送請(qǐng)求到請(qǐng)求發(fā)送成功,其實(shí)這個(gè)過(guò)程只是將file文件轉(zhuǎn)formData請(qǐng)求接口的過(guò)程,真實(shí)的文件處理進(jìn)度并不能通過(guò)自帶的進(jìn)度條直接展示,需要調(diào)用后端的接口獲取上傳進(jìn)度,遍歷后端提供的上傳進(jìn)度接口,使用定時(shí)器,每隔一段時(shí)間調(diào)用一次,獲取最新的上傳進(jìn)度,當(dāng)上傳進(jìn)度為100%時(shí)停止定時(shí)器。

// 上傳文件成功的鉤子    
onSuccess(response, file, fileList) {
      if (response.code == 200) {
        this.fileInfo = true;
        // 上傳進(jìn)度處理,定時(shí)器循環(huán)調(diào)用
        let times = setInterval(() => {
          tailProgressAPI(this.uploadData).then((res) => {
            // handleFileCount 已處理數(shù)量
            // totalFileCount 上傳總數(shù)
            // 錯(cuò)誤文件數(shù)大于0,更新錯(cuò)誤列表
            if (res.data.uploadFailList.length > 0) {
              this.errorList = [];
              this.errorFile = true;
              this.errorList.push(...res.data.uploadFailList);
            }
            // 處理數(shù) = 上傳總數(shù) 處理完成
            if (res.data.handleFileCount == res.data.totalFileCount) {
              // 頁(yè)面展示處理結(jié)果
              this.progress.totalFileCount = res.data.totalFileCount;
              this.progress.handleFileCount = res.data.handleFileCount;
                // 回傳上傳結(jié)果
              this.$emit("uploadBack", { code: 200, msg: "操作成功" });
              // 停止定時(shí)器
              clearInterval(times);
            } else {
              // 處理中
              // 更新處理結(jié)果
              this.progress.totalFileCount = res.data.totalFileCount;
              this.progress.handleFileCount = res.data.handleFileCount;
            }
          });
        }, 500);
      } else {
        file.status = "error"; // 更改類型,關(guān)聯(lián)css的字體變紅
        this.$message.error(`${response.msg}`); // 提示錯(cuò)誤信息
      }
    },

以上就是JS實(shí)現(xiàn)Excel文件與圖片視頻上傳的詳細(xì)內(nèi)容,更多關(guān)于JS文件上傳的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論