JS實(shí)現(xiàn)Excel文件與圖片視頻上傳
Excel上傳
excel的上傳其實(shí)分為兩步:
1、下載excel模板
2、上傳excel模板
在項(xiàng)目中涉及到excel的業(yè)務(wù),基本上都要先下載excel模板,用戶根據(jù)下載的模板填寫excel信息,然后將信息上傳到后臺。下面就這兩部分別說明:
1、下載excel模板
關(guān)于下載excel模板的內(nèi)容請看:調(diào)用后臺接口實(shí)現(xiàn)Excel導(dǎo)出功能以及導(dǎo)出亂碼問題解決(點(diǎn)擊直達(dá))
2、上傳excel模板
這里用到element 的上傳組件,element上傳組件提供點(diǎn)擊上傳和拖動文件上傳。

<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: {}, // 請求頭
fileInfo: false, // 上傳文件
errorList: [], // 錯誤列表
progress: {
totalFileCount: 0, // 總文件
handleFileCount: 0, // 上傳文件
},
fileList: [], // 上傳展示的文件列表,如果fileLimit = 1,該數(shù)組只能有一個成員
};
},
created() {
this.headers["Authorization"] = "Bearer " + getToken(); // 獲取請求頭信息
},
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é)接受上傳的文件、上傳地址、上傳類型,做通用化處理,在任何需要上傳功能的頁面引入即可使用,下面將組件拆開來講:
<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的上傳組件允許我們在上傳的時候攜帶上傳參數(shù),這里的 :data 就是上傳攜帶的參數(shù),該參數(shù)由父組件提供(主要業(yè)務(wù)),這樣就可以在多種不同情況下使用,避免參數(shù)固定化。
:action:請求地址,這個請求地址就是后臺的接口,但是這個接口不能直接使用,需要在接口前加 process.env.VUE_APP_BASE_API 來判斷當(dāng)前的運(yùn)行環(huán)境。
:acceptType: 接受文件的類型,例如這里上傳 .xlsx 文件,那么點(diǎn)擊上傳的時候,只會讀取系統(tǒng)里 .xlsx 的文件。該類型也是由父頁面提供,這樣可以區(qū)分多種文件上傳情況,該組件可以適配多種文件類型,例如:.jpg、.png、.xlsx、.mp4等。
:headers:請求頭,需要加上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ù)時的鉤子,可以做錯誤提示
file-list:上傳文件的數(shù)組
before-remove:上前的事件
on-progress: 上傳中的事件,該事件在上傳過程中會持續(xù)調(diào)用,可以用來做進(jìn)度條展示。
on-success:上傳成功的事件,當(dāng)接口返回200,觸發(fā)該事件。
on-error: 上傳失敗的事件,當(dāng)接口返回500,觸發(fā)該事件。
頁面中的使用
<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ū)");
}
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,
},
};通過父組件將自身業(yè)務(wù)所需的參數(shù)傳入到上傳組件中,具體的上傳業(yè)務(wù)由組件完成,完成后將結(jié)果返回給父組件展示或者刷新展示列表。
圖片和視頻
上文中上傳excel的組件,其實(shí)已經(jīng)可以用作圖片和視頻上傳了,我們只需要傳入對應(yīng)的圖片或視頻接口 actionUrl ,然后將對應(yīng)的上傳類型 acceptType:'.mp4'提供給組件,在選擇文件時就只讀取.mp4的文件,然后將上傳所需的參數(shù) uploadData 提供給組件,那么圖片和視頻上傳就已經(jīng)完成了。
上傳圖片和視頻并不需要模板,直接上傳即可,所以會比較方便。其實(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=>{
//....
})上傳的錯誤提醒以及邏輯處理
某些情況下,我們需要對上傳文件做邏輯處理,比如上傳失敗該如何處理、上傳成功如何處理、能上傳幾個文件、將上傳文件的列表做刪除、上傳中的進(jìn)度處理等,這些邏輯,組件都提供了對應(yīng)的鉤子,如下:
created() {
this.headers["Authorization"] = "Bearer " + getToken();
},
methods: {
// 上傳成功的鉤子,某些情況下需要做邏輯處理
// 上傳5個文件,成功3個,失敗2個,這個時候需要在這里做上傳失敗的文件展示
onSuccess(response, file, fileList) {
if (response.code == 200) {
// 如果失敗列表為數(shù)組證明有上傳失敗
if (Array.isArray(response.data)) {
// 上傳失敗列表數(shù)大于0表示有失敗文件
if (response.data.length > 0) {
this.fileInfo = true; // 打開錯誤列表展示彈窗
file.status = "error"; // 更改類型,關(guān)聯(lián)css的字體變紅
this.errorList = [];
this.errorList.push(...response.data); // 錯誤列表數(shù)據(jù)
this.$message.error(`操作失敗`);
} else {
// 如果錯誤列表為空,則表示全部上傳成功
this.$emit("uploadBack");
}
} else {
// 全部上傳成功
this.$emit("uploadBack");
}
} else {
// 上傳失敗
this.errorList = [];
// 錯誤列表數(shù)據(jù)
this.errorList.push({
fileName: '',
error: response.msg
});
this.fileInfo = true; // 打開錯誤列表展示彈窗
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)閉錯誤信息彈窗
})
.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ā)送請求到請求發(fā)送成功,其實(shí)這個過程只是將file文件轉(zhuǎn)formData請求接口的過程,真實(shí)的文件處理進(jìn)度并不能通過自帶的進(jìn)度條直接展示,需要調(diào)用后端的接口獲取上傳進(jìn)度,遍歷后端提供的上傳進(jìn)度接口,使用定時器,每隔一段時間調(diào)用一次,獲取最新的上傳進(jìn)度,當(dāng)上傳進(jìn)度為100%時停止定時器。
// 上傳文件成功的鉤子
onSuccess(response, file, fileList) {
if (response.code == 200) {
this.fileInfo = true;
// 上傳進(jìn)度處理,定時器循環(huán)調(diào)用
let times = setInterval(() => {
tailProgressAPI(this.uploadData).then((res) => {
// handleFileCount 已處理數(shù)量
// totalFileCount 上傳總數(shù)
// 錯誤文件數(shù)大于0,更新錯誤列表
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) {
// 頁面展示處理結(jié)果
this.progress.totalFileCount = res.data.totalFileCount;
this.progress.handleFileCount = res.data.handleFileCount;
// 回傳上傳結(jié)果
this.$emit("uploadBack", { code: 200, msg: "操作成功" });
// 停止定時器
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}`); // 提示錯誤信息
}
},以上就是JS實(shí)現(xiàn)Excel文件與圖片視頻上傳的詳細(xì)內(nèi)容,更多關(guān)于JS文件上傳的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Javascript單元測試框架QUnitjs詳細(xì)介紹
這篇文章主要介紹了Javascript單元測試框架QUnitjs詳細(xì)介紹,需要的朋友可以參考下2014-05-05
在?localStorage?中上傳和檢索存儲圖像的示例詳解
這篇文章主要介紹了在?localStorage?中上傳和檢索存儲圖像,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2022-06-06
Js實(shí)現(xiàn)手機(jī)發(fā)送驗(yàn)證碼時按鈕延遲操作
在做項(xiàng)目的時候,經(jīng)常遇到發(fā)短信驗(yàn)證碼的問題,這時候需要用戶點(diǎn)完發(fā)送驗(yàn)證碼按鈕后,一段時間內(nèi)不能重復(fù)點(diǎn)擊,畢竟驗(yàn)證碼都是收費(fèi)的嘛,誰都不想浪費(fèi),那么如何實(shí)現(xiàn)這種功能呢?下面來分享一下。2014-06-06
JavaScript判斷是否為數(shù)組的3種方法及效率比較
這篇文章主要介紹了JavaScript判斷是否為數(shù)組的3種方法及效率比較,本文直接給出運(yùn)行效果和實(shí)現(xiàn)代碼,需要的朋友可以參考下2015-04-04
利用google提供的API(JavaScript接口)獲取網(wǎng)站訪問者IP地理位置的代碼詳解
利用google提供的API(JavaScript接口)獲取網(wǎng)站訪問者IP地理位置2010-07-07

