上传新增进度显示、环境变量新增默认上传地址配置

This commit is contained in:
不做码农 2021-12-19 12:36:16 +08:00
parent 9ae54c1799
commit ab6e99d721
6 changed files with 53 additions and 20 deletions

View File

@ -9,3 +9,6 @@ VUE_APP_BASE_API = '/dev-api'
# 路由前缀 # 路由前缀
VUE_APP_ROUTER_PREFIX = '/' VUE_APP_ROUTER_PREFIX = '/'
# 默认上传地址
VUE_APP_UPLOAD_URL = '/Common/UploadFile'

View File

@ -9,3 +9,6 @@ VUE_APP_BASE_API = '/prod-api'
# 路由前缀 # 路由前缀
VUE_APP_ROUTER_PREFIX = '/' VUE_APP_ROUTER_PREFIX = '/'
# 默认上传地址
VUE_APP_UPLOAD_URL = '/Common/UploadFile'

View File

@ -9,3 +9,6 @@ VUE_APP_BASE_API = '/stage-api'
# 路由前缀 # 路由前缀
VUE_APP_ROUTER_PREFIX = '/' VUE_APP_ROUTER_PREFIX = '/'
# 默认上传地址
VUE_APP_UPLOAD_URL = '/Common/UploadFile'

View File

@ -1,8 +1,8 @@
<template> <template>
<div class="upload-file"> <div class="upload-file">
<el-upload :action="uploadFileUrl" :before-upload="handleBeforeUpload" :file-list="fileList" :limit="limit" :on-error="handleUploadError" <el-upload :action="uploadFileUrl" :before-upload="handleBeforeUpload" :file-list="fileList" :limit="limit" :on-error="handleUploadError"
:on-exceed="handleExceed" :on-success="handleUploadSuccess" :show-file-list="false" :data="data" :headers="headers" class="upload-file-uploader" :on-exceed="handleExceed" :on-success="handleUploadSuccess" :on-progress="uploadProcess" :show-file-list="false" :data="data" :headers="headers"
ref="upload"> class="upload-file-uploader" ref="upload">
<!-- 上传按钮 --> <!-- 上传按钮 -->
<el-button size="mini" type="primary" icon="el-icon-upload">选取文件</el-button> <el-button size="mini" type="primary" icon="el-icon-upload">选取文件</el-button>
<!-- 上传提示 --> <!-- 上传提示 -->
@ -12,6 +12,7 @@
<template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template> <template v-if="fileType"> 格式为 <b style="color: #f56c6c">{{ fileType.join("/") }}</b> </template>
的文件 的文件
</div> </div>
<el-progress v-show="showProgress == true" :percentage="uploadPercent" style="margin-top:10px;"></el-progress>
</el-upload> </el-upload>
<!-- 文件列表 --> <!-- 文件列表 -->
@ -59,17 +60,19 @@ export default {
// //
uploadUrl: { uploadUrl: {
type: String, type: String,
default: "/Common/UploadFile", default: process.env.VUE_APP_UPLOAD_URL ?? "/Common/UploadFile",
}, },
// form // form
column: [String], column: [String],
// //
data: { data: {
type: Object type: Object,
} },
}, },
data() { data() {
return { return {
showProgress: false,
uploadPercent: 0,
baseUrl: process.env.VUE_APP_BASE_API, baseUrl: process.env.VUE_APP_BASE_API,
uploadFileUrl: process.env.VUE_APP_BASE_API + this.uploadUrl, // uploadFileUrl: process.env.VUE_APP_BASE_API + this.uploadUrl, //
headers: { headers: {
@ -137,6 +140,7 @@ export default {
return false; return false;
} }
} }
this.showProgress = true;
return true; return true;
}, },
// //
@ -146,11 +150,17 @@ export default {
// //
handleUploadError(err) { handleUploadError(err) {
this.msgError("上传失败, 请重试"); this.msgError("上传失败, 请重试");
this.fileList = [];
this.showProgress = false;
this.uploadPercent = 0;
}, },
// //
handleUploadSuccess(res, file) { handleUploadSuccess(res, file) {
this.showProgress = false;
this.uploadPercent = 0;
if (res.code != 200) { if (res.code != 200) {
this.fileList = []; this.fileList = [];
this.msgError(`上传失败,原因:${res.msg}!`); this.msgError(`上传失败,原因:${res.msg}!`);
return; return;
} }
@ -158,6 +168,12 @@ export default {
this.fileList.push({ name: res.data.fileName, url: res.data.url }); this.fileList.push({ name: res.data.fileName, url: res.data.url });
this.$emit("input", this.column, this.listToString(this.fileList)); this.$emit("input", this.column, this.listToString(this.fileList));
}, },
//
uploadProcess(event, file, fileList) {
this.showProgress = true;
this.videoUploadPercent = file.percentage.toFixed(0);
console.log("上传进度" + file.percentage);
},
// //
handleDelete(index) { handleDelete(index) {
this.fileList.splice(index, 1); this.fileList.splice(index, 1);

View File

@ -2,9 +2,10 @@
<div class="component-upload-image"> <div class="component-upload-image">
<el-upload list-type="picture-card" :action="uploadImgUrl" :on-success="handleUploadSuccess" :before-upload="handleBeforeUpload" <el-upload list-type="picture-card" :action="uploadImgUrl" :on-success="handleUploadSuccess" :before-upload="handleBeforeUpload"
:on-exceed="handleExceed" :on-remove="handleRemove" :on-error="handleUploadError" name="file" :show-file-list="true" :limit="limit" :on-exceed="handleExceed" :on-remove="handleRemove" :on-error="handleUploadError" name="file" :show-file-list="true" :limit="limit"
:file-list="fileList" :on-preview="handlePictureCardPreview" :class="{hide: this.fileList.length >= this.limit}" :headers="headers"> :file-list="fileList" :on-preview="handlePictureCardPreview" :on-progress="uploadProcess" :class="{hide: this.fileList.length >= this.limit}"
:headers="headers">
<i class="el-icon-plus"></i> <i class="el-icon-plus"></i>
<el-progress v-if="showProgress == true" type="circle" :percentage="uploadPercent" style="margin-top:10px;"></el-progress>
<!-- 上传提示 --> <!-- 上传提示 -->
<div class="el-upload__tip" slot="tip" v-if="showTip"> <div class="el-upload__tip" slot="tip" v-if="showTip">
请上传 请上传
@ -34,7 +35,7 @@ export default {
// //
uploadUrl: { uploadUrl: {
type: String, type: String,
default: "/Common/UploadFile", default: process.env.VUE_APP_UPLOAD_URL ?? "/Common/UploadFile",
}, },
// , ['png', 'jpg', 'jpeg'] // , ['png', 'jpg', 'jpeg']
fileType: { fileType: {
@ -59,6 +60,8 @@ export default {
dialogImageUrl: "", dialogImageUrl: "",
dialogVisible: false, dialogVisible: false,
hideUpload: false, hideUpload: false,
showProgress: false,
uploadPercent: 0,
uploadImgUrl: process.env.VUE_APP_BASE_API + this.uploadUrl, // uploadImgUrl: process.env.VUE_APP_BASE_API + this.uploadUrl, //
headers: { headers: {
Authorization: "Bearer " + getToken(), Authorization: "Bearer " + getToken(),
@ -112,7 +115,9 @@ export default {
// //
handleUploadSuccess(res) { handleUploadSuccess(res) {
console.log(res); console.log(res);
this.loading.close(); this.showProgress = false;
this.uploadPercent = 0;
if (res.code != 200) { if (res.code != 200) {
this.msgError(`上传失败,原因:${res.msg}!`); this.msgError(`上传失败,原因:${res.msg}!`);
return; return;
@ -150,11 +155,7 @@ export default {
return false; return false;
} }
} }
this.loading = this.$loading({ this.showProgress = true;
lock: true,
text: "上传中",
background: "rgba(0, 0, 0, 0.7)",
});
}, },
// //
handleExceed() { handleExceed() {
@ -179,7 +180,14 @@ export default {
type: "error", type: "error",
message: "上传失败", message: "上传失败",
}); });
this.loading.close(); this.showProgress = false;
this.uploadPercent = 0;
},
//
uploadProcess(event, file, fileList) {
this.showProgress = true;
this.videoUploadPercent = file.percentage.toFixed(0);
console.log("上传进度" + file.percentage);
}, },
}, },
}; };

View File

@ -77,4 +77,4 @@ new Vue({
store, store,
render: h => h(App) render: h => h(App)
}) })
console.log('后端地址:' + process.env.VUE_APP_BASE_API) console.log('环境变量:', process.env)