上传
<template>
<div>
<el-upload
ref="upload"
:limit="CountNum"
:on-remove="handleRemove"
:on-error="onError"
:file-list="urlArr"
:on-success="handleAvatarSuccess"
:before-upload="beforeAvatarUpload"
:action="url"
:on-preview="tabPreview"
class="upload-demo"
style="width: 450px"
>
<el-button slot="trigger" size="small" type="primary">上传附件</el-button>
<div slot="tip" class="el-upload__tip">
支持上传 {{ strRebuild(fileType) }} 格式,且不超过 {{ fileSize }}M
</div>
</el-upload>
<FilePreview ref="filePreview"> </FilePreview>
</div>
</template>
<script>
import { strRebuild, lastSubstring } from "@/utils/strUtil";
import { message } from "@/utils/message";
import FilePreview from "@/components/filePreview/index.vue";
export default {
props: {
urlArr: {
type: Array,
default: [],
},
CountNum: {
type: Number,
default: 1,
},
},
components: {
FilePreview,
},
data() {
return {
fileList: [],
fileType: [
"xls",
"xlsx",
"pdf",
"doc",
"docx",
"jpg",
"png",
"jpeg",
],
fileSize: 10,
};
},
created() {
this.url = this.uploadURL;
},
methods: {
clear() {
this.$refs.upload.clearFiles();
},
beforeAvatarUpload(file) {
console.log(file);
var flag = true;
var tip = "";
if (file.size > this.fileSize * 1024 * 1024) {
flag = false;
tip = " 文件超过" + this.fileSize + "M";
}
if (!this.fileType.includes(lastSubstring(file.name, "."))) {
flag = false;
tip = " 文件类型不可上传";
}
if (!flag) {
message("error", tip);
}
return flag;
},
handleAvatarSuccess(res, file) {
if (res.code === 0) {
let arr = this.urlArr;
console.log(res.data);
console.log(this.urlArr);
arr.push({
name: res.data,
url: this.showURL + res.data,
});
this.$notify.success({
title: "成功",
message: "上传成功",
});
this.$emit("imgUrl", arr);
} else {
}
},
checkFile() {
var flag = true;
var tip = "";
var files = this.$refs.upload.uploadFiles;
files.forEach((item) => {
if (item.size > this.fileSize * 1024 * 1024) {
flag = false;
tip = " 文件超过" + this.fileSize + "M";
}
console.log(this.fileType.includes(lastSubstring(item.name, ".")));
if (!this.fileType.includes(lastSubstring(item.name, "."))) {
flag = false;
tip = " 文件类型不可上传";
}
});
if (!flag) {
message("error", tip);
}
return flag;
},
submitUpload() {
if (this.checkFile()) {
console.log("上传附件...");
this.$refs.upload.submit();
} else {
console.log("取消上传");
}
},
tabPreview(file) {
console.log(file);
this.$refs.filePreview.previewPdf(file);
},
uploadFile(file) {
},
handleRemove(file, fileList) {
console.log("移除附件...", file, fileList);
let url = file.name;
let index = this.urlArr.findIndex((v) => {
return url === v.name;
});
this.urlArr.splice(index, 1);
this.$emit("imgUrl", this.urlArr);
},
onError(err) {
message("error", "附件上传失败");
console.log(err);
},
strRebuild(str) {
return strRebuild(str);
},
},
};
</script>
<style lang="scss" scoped>
</style>
预览
<template>
<div>
<el-button @click="previewPdf()" type="text" size="small">
<div class="images" v-viewer="{ navbar: false }">
<img
v-for="(src, index) in images"
style="width: 100%"
:src="src"
:key="src"
hidden
/>
<!-- 预览 -->
</div>
</el-button>
<el-dialog
:visible="showDoc == true || showPdf == true || showVideo == true"
:show-close="true"
:width="'80%'"
top="5vh"
class="dialog-div-pre-style"
:before-close="closePreviewClick"
center
>
<div v-if="showDoc == true" class="dialog-body-content-base-style">
<iframe
frameborder="0"
:src="
'https://view.officeapps.live.com/op/view.aspx?src=' + this.fileUrl
"
width="100%"
style="min-height: 800px; height: 100%"
>
</iframe>
</div>
<div
v-else-if="showPdf == true"
class="dialog-body-content-base-style"
style="justify-content: center; align-items: center"
>
<embed
:src="pdfUrl"
style="width: 100%; min-height: 800px; height: 100%"
/>
</div>
<div
v-else-if="showVideo == true"
class="dialog-body-content-base-style"
style="justify-content: center; align-items: center"
>
<video-player
class="video-player vjs-custom-skin"
ref="positiveVideoPlayer"
:playsinline="true"
:options="positivePlayerOptions"
></video-player>
</div>
</el-dialog>
</div>
</template>
<script>
export default {
data() {
return {
showDoc: false,
showPdf: false,
showVideo: false,
fileUrl: "",
images: [],
currentPage: 0,
pageCount: 0,
pdfUrl: "",
scale: 1.0,
positivePlayerOptions: {
playbackRates: [0.5, 1.0, 1.5, 2.0],
autoplay: false,
muted: false,
loop: false,
preload: "auto",
language: "zh-CN",
aspectRatio: "16:9",
fluid: true,
sources: [
{
type: "",
src: "",
},
],
poster: "",
notSupportedMessage: "此视频暂无法播放,请稍后再试",
controlBar: {
timeDivider: true,
durationDisplay: true,
remainingTimeDisplay: false,
fullscreenToggle: true,
},
common: "positivePlayer",
},
};
},
methods: {
previewPdf(row) {
let type = this.iconByType(row);
this.fileUrl = row.url;
if (
type.indexOf("doc") != -1 ||
type.indexOf("docx") != -1 ||
type.indexOf("xsl") != -1 ||
type.indexOf("xslx") != -1
) {
this.showDoc = true;
} else if (type.indexOf("pdf") != -1) {
this.pdfUrl = row.url;
this.showPdf = true;
} else if (
type.indexOf("jpg") != -1 ||
type.indexOf("png") != -1 ||
type.indexOf("jpeg") != -1
) {
this.images = [row.url];
const viewer = this.$el.querySelector(".images").$viewer;
viewer.show();
} else if (
type.indexOf("avi") != -1 ||
type.indexOf("mp4") != -1 ||
type.indexOf("webm") != -1 ||
type.indexOf("m4v") != -1 ||
type.indexOf("rmvb") != -1 ||
type.indexOf("mpg") != -1 ||
type.indexOf("3gp") != -1 ||
type.indexOf("swf") != -1 ||
type.indexOf("mkv") != -1
) {
this.positivePlayerOptions.sources[0].src = row.url;
this.positivePlayerOptions.sources[0].type = "video/mp4";
this.showVideo = true;
} else {
this.$message("当前文件暂不支持预览");
}
},
iconByType(row) {
return row.name.substring(row.name.lastIndexOf(".") + 1, row.name.length);
},
closePreviewClick() {
if (this.showDoc == true) {
this.showDoc = false;
} else if (this.showPdf == true) {
this.showPdf = false;
} else if (this.showVideo == true) {
this.showVideo = false;
}
},
},
};
</script>