文件上传
This commit is contained in:
1 parent
d418e9b853
commit
ae997fe850
6 files changed
+1584
No files matched your search
@@ -0,0 +1,150 @@
|
||||
<template id="file-upload">
|
||||
<v-container class="fullScreen d-flex flex-column pa-xs-0 pa-0">
|
||||
<v-file-input multiple v-model="inputFile" hide-input dense filled single-line style="width: 10px;" color="red"
|
||||
label="请选择文件"></v-file-input>
|
||||
<v-card outlined class="mt-2" v-if="value && value.length > 0">
|
||||
<v-data-table :headers="headers" :items="value"
|
||||
:footer-props="{ itemsPerPageOptions: [20, 40, 60, 100, -1] }" :items-per-page="100" :loading="isTableLoading"
|
||||
mobile-breakpoint="0" :class="{'mx-2': isMobile}"
|
||||
class="elevation-0 mt-0 flex-fill d-flex flex-column">
|
||||
<template v-slot:item.download-path="{ item }">
|
||||
<preview-file-group :file-list="[item]" />
|
||||
</template>
|
||||
<template v-slot:item.action="{ item, index }">
|
||||
<span role="button" class="success--text font-weight-medium font-size-2"
|
||||
@click="doUiAction('deleteItem', { item, index })">
|
||||
<v-icon size="14" class="success--text">mdi-trash-can-outline</v-icon>删除
|
||||
</span>
|
||||
</template>
|
||||
<template v-slot:item.operationAt="{ item }">
|
||||
{{ item.operationAt && dayjs(item.operationAt).format('YYYY-MM-DD HH:mm:ss') }}
|
||||
</template>
|
||||
</v-data-table>
|
||||
</v-card>
|
||||
|
||||
</v-container>
|
||||
</template>
|
||||
|
||||
{% include 'component/common/previewFileGroup.html' %}
|
||||
|
||||
<script type="module">
|
||||
Vue.component("file-upload", {
|
||||
template: '#file-upload',
|
||||
props: {
|
||||
fileBizId: {
|
||||
type: Array,
|
||||
default: null,
|
||||
},
|
||||
value: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
},
|
||||
},
|
||||
vuetify: new Vuetify(),
|
||||
data: () => ({
|
||||
isMobile: window.innerWidth < 600,
|
||||
|
||||
serverSearchInput: {
|
||||
rowStatus: ['正常']
|
||||
},
|
||||
// 表格相关数据
|
||||
requireRules: [v => !!v || '必填'],
|
||||
constantCollection: {},
|
||||
isTableLoading: false,
|
||||
headers: [
|
||||
{ text: "文件预览/下载", value: "download-path", align: 'center', sortable: false, width: 150, },
|
||||
{ text: "文件名", value: "filename", width: 120, class: 'fixed' },
|
||||
{ text: "文件大小(KB)", value: "binarySize", width: 100 },
|
||||
{ text: '操作', value: 'action', align: 'center', sortable: false, width: 150, },
|
||||
],
|
||||
|
||||
inputFile: [],
|
||||
}),
|
||||
computed: {
|
||||
isMobile() {
|
||||
return window.innerWidth < 600;
|
||||
},
|
||||
},
|
||||
watch: {
|
||||
'inputFile':{
|
||||
handler(newVal,oldVal){
|
||||
console.log('inputFile', newVal);
|
||||
newVal.forEach(element => {
|
||||
this.uploadFn(element)
|
||||
});
|
||||
}
|
||||
}
|
||||
},
|
||||
async created() {
|
||||
},
|
||||
mounted() {
|
||||
},
|
||||
methods: {
|
||||
async doUiAction(uiActionId, uiActionData) {
|
||||
switch (uiActionId) {
|
||||
case 'deleteItem':
|
||||
await this.confirmDeleteItemDialog(uiActionData);
|
||||
await this.doDeleteItem(uiActionData);
|
||||
break;
|
||||
default:
|
||||
console.error("[doUiAction] uiActionId not find", { uiActionId });
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
async uploadFn(file){
|
||||
this.isTableLoading = true
|
||||
const fileBizId = this.fileBizId;
|
||||
await window.vtoast.loading("保存中");
|
||||
window.vtoast.loading(`${file.name} 上传进度: 0.00%`);
|
||||
const result = await window.jianghuAxios.httpUploadByStream({
|
||||
file,
|
||||
fileDirectory: `test`,
|
||||
onProgress: (total, loaded) => {
|
||||
const progress = Number((loaded * 100 / total).toFixed(2));
|
||||
window.vtoast.loading(`${file.name} 上传进度: ${progress}%`);
|
||||
if (loaded === total) {
|
||||
window.vtoast.success(`${file.name} 上传进度: 100%`);
|
||||
}
|
||||
}
|
||||
})
|
||||
console.log('uploadFn', result);
|
||||
|
||||
const { filename, downloadPath, binarySize } = result.data.appData.resultData;
|
||||
this.$emit('input', [...this.value, {filename, downloadPath, binarySize}]);
|
||||
|
||||
const { status } = result.data;
|
||||
if (status === 'success') {
|
||||
await window.jianghuAxios({
|
||||
data: {
|
||||
appData: {
|
||||
pageId: 'allPage',
|
||||
actionId: 'insertItem',
|
||||
actionData: { filename, downloadPath, binarySize }
|
||||
}
|
||||
}
|
||||
})
|
||||
} else {
|
||||
// this.uploadFail++
|
||||
await window.vtoast.fail(filename + "上传失败");
|
||||
}
|
||||
this.isTableLoading = false
|
||||
},
|
||||
|
||||
// deleteItem
|
||||
async confirmDeleteItemDialog({ item:{ filename } }) {
|
||||
if (await window.confirmDialog({ title: "删除", content: "确定删除文件" + filename + "吗?" }) === false) {
|
||||
throw new Error("[confirmDeleteItemDialog] 否");
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* 删除数据
|
||||
*/
|
||||
// TODO::只是删除了value的数据,并没有删除文件
|
||||
async doDeleteItem({index}) {
|
||||
this.$emit('input', this.value.filter((f, i) => index != i))
|
||||
},
|
||||
}
|
||||
})
|
||||
</script>
|
||||
@@ -0,0 +1,216 @@
|
||||
<template id="preview-file-group">
|
||||
<v-container class="pa-0">
|
||||
<div class="img-group" v-if="false">
|
||||
<div class="img" v-for="(item, index) in fileList" @click="doUiAction('previewFile', { item })"
|
||||
:style="`background-image: url(/<$ ctx.app.config.appId $>/upload${item.downloadPath})`"></div>
|
||||
</div>
|
||||
<div class="file-group">
|
||||
<div class="file" v-for="(item, index) in fileList">
|
||||
<span small role="button" class="success--text font-weight-medium font-size-2 mr-1 mr-sm-2"
|
||||
v-if="item.downloadPath" @click="doUiAction('previewFile', { item })">查看</span>
|
||||
<span small role="button" class="success--text font-weight-medium font-size-2 mr-1 mr-sm-2"
|
||||
v-if="item.downloadPath" @click="doUiAction('downloadFile', { item })">下载</span>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- 文件预览 -->
|
||||
<v-overlay v-if="previewFileType === 'pdf'" :value="previewOverlay" @click="previewOverlay = false" :opacity="0.85" style="z-index: 49999">
|
||||
<v-icon style="position: fixed; right: 10px; top: 5px; z-index: 50000" large color="green"
|
||||
@click="previewOverlay = false">
|
||||
mdi-close-circle
|
||||
</v-icon>
|
||||
|
||||
<iframe :src="'/<$ ctx.app.config.appId $>/public/pdf/web/viewer.html?file=' + previewFileUrl" frameborder="0"
|
||||
style="width: 75vw; height: 100vh; padding: 50px 0 0 0;"></iframe>
|
||||
</v-overlay>
|
||||
|
||||
<viewer ref="viewer" :images="previewFileUrl" class="images clearfix">
|
||||
<template slot-scope="scope">
|
||||
<img style="display: none" :src="previewFileUrl" class="image">
|
||||
</template>
|
||||
</viewer>
|
||||
</v-container>
|
||||
</template>
|
||||
|
||||
<link href="/<$ ctx.app.config.appId $>/public/lib/viewerjs/viewer.min.css" rel="stylesheet">
|
||||
<script src="/<$ ctx.app.config.appId $>/public/lib/viewerjs/viewer.min.js"></script>
|
||||
<script src="/<$ ctx.app.config.appId $>/public/lib/viewerjs/v-viewer.min.js"></script>
|
||||
|
||||
|
||||
<script type="module">
|
||||
Vue.use(VueViewer.default)
|
||||
Vue.component("preview-file-group", {
|
||||
template: '#preview-file-group',
|
||||
props: {
|
||||
fileList: {
|
||||
type: Array,
|
||||
default: () => [],
|
||||
}
|
||||
},
|
||||
mounted() {
|
||||
|
||||
},
|
||||
data: () => ({
|
||||
previewPrefix: `/<$ ctx.app.config.appId $>/upload`,
|
||||
previewOverlay: false,
|
||||
previewFileUrl: '',
|
||||
previewFileBase64: '',
|
||||
previewFilename: '',
|
||||
previewFileType: '',
|
||||
downloadPath: '',
|
||||
}),
|
||||
methods: {
|
||||
async doUiAction(uiActionId, uiActionData) {
|
||||
switch (uiActionId) {
|
||||
case 'previewFile':
|
||||
await this.previewFile(uiActionData);
|
||||
break;
|
||||
case 'downloadFile':
|
||||
await this.downloadFile(uiActionData);
|
||||
break;
|
||||
case 'downloadPreviewFile':
|
||||
await this.downloadPreviewFile(uiActionData);
|
||||
break;
|
||||
case 'imgWheelFn':
|
||||
await this.imgWheelFn(uiActionData)
|
||||
break;
|
||||
default:
|
||||
console.error("[doUiAction] uiActionId not find", { uiActionId });
|
||||
break;
|
||||
}
|
||||
},
|
||||
|
||||
/**
|
||||
* uiActionId: downloadFile
|
||||
* description: ✅下载
|
||||
* main: [{"function":"downloadFile"}]
|
||||
*/
|
||||
async downloadFile({ item }) {
|
||||
const { filename, downloadPath } = item;
|
||||
window.vtoast.loading('文件下载进度0.00%');
|
||||
const buffer = await window.jianghuAxios.httpDownloadByStream({
|
||||
downloadPath, filename,
|
||||
onProgress: (total, loaded) => {
|
||||
const progress = Number((loaded * 100 / total).toFixed(2));
|
||||
window.vtoast.loading(`文件下载进度${progress}%`);
|
||||
if (progress === 100) {
|
||||
window.vtoast.success('文件下载成功');
|
||||
}
|
||||
}
|
||||
}).catch(err => {
|
||||
window.vtoast.fail({ message: err.errorReason });
|
||||
throw err;
|
||||
});
|
||||
window.jianghuAxios.downloadBufferToChrome({ buffer, filename });
|
||||
},
|
||||
/**
|
||||
* uiActionId: previewFile
|
||||
* description: ✅预览文件
|
||||
* main: [{"function":"previewFile"}]
|
||||
*/
|
||||
async previewFile({ item }) {
|
||||
const { filename, downloadPath, binarySize } = item;
|
||||
if (binarySize > 50 * 1024) {
|
||||
window.vtoast.fail('文件大于50M, 不支持预览!');
|
||||
// 中断uiAction
|
||||
return false;
|
||||
}
|
||||
this.filename = filename;
|
||||
if (/\.(gif|jpg|jpeg|png|GIF|JPG|PNG)$/.test(filename)) {
|
||||
this.previewFileType = 'img';
|
||||
this.previewFileUrl = `/<$ ctx.app.config.appId $>/upload${downloadPath}`;
|
||||
|
||||
this.$refs.viewer.$viewer.show()
|
||||
|
||||
}
|
||||
if (/\.(pdf|PDF)$/.test(filename)) {
|
||||
this.previewFileType = 'pdf';
|
||||
this.previewFileUrl = encodeURIComponent(`/<$ ctx.app.config.appId $>/upload${downloadPath}`);
|
||||
}
|
||||
|
||||
if (/\.(xls|xlsx)$/.test(filename)){
|
||||
this.previewFileType = 'excel';
|
||||
window.vtoast.success('excel文件不能预览');
|
||||
}
|
||||
this.previewFilename = filename;
|
||||
this.previewOverlay = true;
|
||||
this.downloadPath = downloadPath;
|
||||
|
||||
},
|
||||
|
||||
/**
|
||||
* uiActionId: downloadPreviewFile
|
||||
* description: ✅下载正在预览的文件
|
||||
* main: [{"function":"downloadPreviewFile"}]
|
||||
*/
|
||||
async downloadPreviewFile() {
|
||||
const oReq = new XMLHttpRequest();
|
||||
const url = `/<$ ctx.app.config.appId $>/upload${this.downloadPath}`;
|
||||
const filename = this.previewFilename;
|
||||
oReq.open("GET", url, true);
|
||||
oReq.responseType = "arraybuffer";
|
||||
oReq.onprogress = function (event) {
|
||||
if (event.lengthComputable) {
|
||||
const progress = Number((event.loaded * 100 / event.total).toFixed(2));
|
||||
window.vtoast.loading(`文件下载进度${progress}%`);
|
||||
if (progress === 100) {
|
||||
window.vtoast.success('文件下载成功');
|
||||
}
|
||||
}
|
||||
}
|
||||
oReq.onload = function (oEvent) {
|
||||
const buffer = oReq.response;
|
||||
let url = window.URL.createObjectURL(new Blob([buffer], { type: "arraybuffer" }))
|
||||
const link = document.createElement('a');
|
||||
link.style.display = 'none';
|
||||
link.href = url;
|
||||
link.setAttribute('download', filename);
|
||||
document.body.appendChild(link);
|
||||
link.click();
|
||||
document.body.removeChild(link);
|
||||
};
|
||||
oReq.send(null);
|
||||
},
|
||||
}
|
||||
})
|
||||
</script>
|
||||
<style>
|
||||
.add-btn {
|
||||
width: 100%;
|
||||
margin-top: 5px;
|
||||
padding: 5px;
|
||||
}
|
||||
|
||||
.img-group {
|
||||
display: flex;
|
||||
}
|
||||
|
||||
.img-group .img {
|
||||
width: 80px;
|
||||
height: 80px;
|
||||
background-size: cover;
|
||||
margin-right: 5px;
|
||||
cursor: pointer;
|
||||
}
|
||||
|
||||
.img-group .img :last-child {
|
||||
margin-right: 0;
|
||||
}
|
||||
|
||||
.file-group {
|
||||
margin: 5px 0;
|
||||
}
|
||||
|
||||
.file-group .file {
|
||||
margin-bottom: 5px;
|
||||
white-space: nowrap;
|
||||
}
|
||||
|
||||
.file-group .file:last-child {
|
||||
margin-bottom: 0;
|
||||
}
|
||||
|
||||
.v-overlay {
|
||||
overflow: hidden;
|
||||
}
|
||||
</style>
|
||||
Reference in new issue
Block a user