vue-upload上傳圖片詳細使用方法
前言
實際開發(fā)中我們的圖片,文件,PDF我們都應(yīng)該存在文檔服務(wù)器當中。從而優(yōu)化代碼,減少代碼文件大小。
我們可以讓后端把文檔服務(wù)器搭建好,寫好相應(yīng)的存儲接口api,我們就可以使用Upload組件上傳。
但是我們需要注意的是,在實際開發(fā)中。在請求攔截中設(shè)置的token,和userid(用戶id)要重新設(shè)置一次。
因為我們是使用upload組件直接上傳,并沒有經(jīng)過axios,攔截不到。要在headers(請求頭中設(shè)置2個參數(shù))
代碼實現(xiàn)
1.在添加表單中使用upload組件(餓了嗎)
<el-form-item label="維保打印記錄:" prop="fileList">
<!-- <el-input v-model="form.mcRecord" style="width: 350px"></el-input> -->
<!-- list-type="picture" 上傳圖片的樣式 -->
<el-upload
class="upload-demo"
:action="upload.url"
:on-preview="handlePreview"
:headers="upload.headers"
:on-remove="handleRemove"
:on-success="handleFileSuccess"
:file-list="fileList"
list-type="picture"
:limit="10"
:on-exceed="handleExceed"
>
<el-button size="small" type="primary">點擊上傳</el-button>
<div slot="tip" class="el-upload__tip">
只能上傳jpg/png文件,且不超過500kb
</div>
</el-upload>
</el-form-item>2.在data中設(shè)置參數(shù)
2.1fileList是上傳成功圖片存儲地方,是一個數(shù)組對象,我是直接轉(zhuǎn)換成數(shù)組字符串存在后端。
2.2url是當前連接的后端地址加上api地址
2.3headers是upload屬性,添加api請求頭中的token和tenant-id(用戶id)來驗證身份。
2.4getToken,getTenantId,是在utils中的方法,獲取token和用戶id的。
// 上傳圖片成功之后存儲地方
fileList: [],
// 圖片上傳地址,和請求頭數(shù)據(jù)
upload: {
// 設(shè)置上傳的請求頭部
headers: { token: getToken(), "tenant-id": getTenantId() },
// 上傳的地址
url: process.env.VUE_APP_BASE_API + "/media/upload/coursefile",
},3.methods中的方法
為了方便查看,在點擊已經(jīng)上傳成功文件時,會動態(tài)的使用js在document創(chuàng)建一個img來展示圖片,方便查看。
// 文件上傳成功鉤子
handleFileSuccess(response, file, fileList) {
console.log("response", response);
console.log("file", file);
console.log("fileList", fileList);
let x = {};
x.name = response.filename;
x.url = response.url;
x.id = response.id;
console.log("上傳圖片", x);
this.fileList.push(x);
},
// 點擊已上傳文件右上角刪除鉤子
handleRemove(file, fileList) {
// console.log("id", file.id);
console.log("刪除之后", fileList);
// const x = this.fileList.findIndex((v) => v.id == file.id);
// console.log("刪除下標", x);
// this.fileList.splice(x, 1);
this.fileList = fileList;
console.log("處理過數(shù)據(jù)", this.fileList);
},
// 文件上傳數(shù)量超過設(shè)置數(shù)量
handleExceed(files, fileList) {
this.$message.warning(`最多只能選擇10張圖片${fileList.length} 個文件`);
},
// 點擊文件列表中已上傳的文件時的鉤子
handlePreview(file) {
console.log(file);
const image = new Image();
image.src = file.url;
image.onload = () => {
// 創(chuàng)建彈出層
console.log("執(zhí)行了");
const previewContainer = document.createElement("div");
previewContainer.style.position = "fixed";
previewContainer.style.top = 0;
previewContainer.style.bottom = 0;
previewContainer.style.left = 0;
previewContainer.style.right = 0;
previewContainer.style.zIndex = 99999;
previewContainer.style.backgroundColor = "rgba(0,0,0,0.8)";
previewContainer.style.display = "flex";
previewContainer.style.justifyContent = "center";
previewContainer.style.alignItems = "center";
document.body.appendChild(previewContainer);
// 在彈出層中添加圖片
const previewImage = document.createElement("img");
previewImage.src = file.url;
previewImage.style.maxWidth = "80%";
previewImage.style.maxHeight = "80%";
previewContainer.appendChild(previewImage);
// 點擊彈出層,關(guān)閉預(yù)覽
previewContainer.addEventListener("click", () => {
document.body.removeChild(previewContainer);
});
};
},注意:
直接復制需要根據(jù)實際情況更改url(api接口路徑),headers中請求頭攜帶的參數(shù)(以request.js文件為準)。
總結(jié):
經(jīng)過這一趟流程下來相信你也對 vue-upload上傳圖片詳細使用(文檔服務(wù)器) 有了初步的深刻印象,但在實際開發(fā)中我 們遇到的情況肯定是不一樣的,所以我們要理解它的原理,萬變不離其宗。加油,打工人!
到此這篇關(guān)于vue-upload上傳圖片詳細使用方法的文章就介紹到這了,更多相關(guān)vue-upload上傳圖片內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3+ts前端封裝EventSource并在請求頭添加token的方法
這篇文章主要介紹了vue3+ts前端封裝EventSource并在請求頭添加token,本文將介紹如何使用 event-source-polyfill 來解決這個問題,需要的朋友可以參考下2024-12-12
vue?element-ui的table列表中展示多張圖片(可放大)效果實例
這篇文章主要給大家介紹了關(guān)于vue?element-ui的table列表中展示多張圖片(可放大)效果的相關(guān)資料,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下2023-08-08
vue3原始值響應(yīng)方案及響應(yīng)丟失問題解讀
這篇文章主要介紹了vue3原始值響應(yīng)方案及響應(yīng)丟失問題解讀,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-04-04
如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用
這篇文章主要教大家如何使用Vuex+Vue.js構(gòu)建單頁應(yīng)用,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-10-10
element中datepicker日期選擇器選擇周一到周日并實現(xiàn)上一周和下一周的方法
最近項目中需要用到日期選擇器,所以這里給大家總結(jié)下,這篇文章主要給大家介紹了關(guān)于element中datepicker日期選擇器選擇周一到周日并實現(xiàn)上一周和下一周的相關(guān)資料,需要的朋友可以參考下2023-09-09
基于vue-cli npm run build之后vendor.js文件過大的解決方法
今天小編就為大家分享一篇基于vue-cli npm run build之后vendor.js文件過大的解決方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-09-09

