123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138 |
- <template>
- <input
- ref="file"
- type="file"
- name="file"
- id
- style="display: none"
- :accept="accept"
- />
- </template>
- <script>
- import * as fileInfo from "@/utils/file";
- export default {
- props: {
- limit: Number,
- mediaType: Array,
- acceptType: String,
- failString: String,
- limitFailStr: String,
- },
- computed: {
- accept() {
- if (this.acceptType) {
- return this.acceptType;
- }
- if (!this.mediaType) {
- return '*'
- }
- let tmp = []
- this.mediaType.forEach(item => {
- tmp = tmp.concat(fileInfo.mediaMimes[item]
- .map((ii) => item + "/" + ii))
- });
- return tmp.join(",");
- },
- },
- mounted() {
- this.$el.addEventListener("change", (e) => {
- if (!window.FileReader) {
- this.$showAlert({
- tips:'无法上传'
- })
- e.target.value = "";
- return;
- }
- if (e.target.files.length === 0) {
- e.target.value = "";
- return;
- }
- this.files = e.target.files;
- this.files = Array.from(this.files).filter((item, i) => {
- if (this.mediaType.length > 0 ) {
- let pass = this.mediaType.find(jj=>{
- return fileInfo.checkMediaMime(jj, item.name)
- })
- if (!pass) {
- setTimeout(() => {
- if (this.failString) {
- this.$showAlert({
- tips:`“${item.name}”` + this.failString
- })
- } else {
- this.$showAlert({
- tips:"不支持该文件格式"
- })
- }
- }, i * 100);
- return;
- }
- }
- if (this.limit > 0) {
- if (!fileInfo.checkSizeLimitFree(item.size, this.limit)) {
- e.target.value = "";
- setTimeout(() => {
- if (this.limitFailStr) {
- this.$showAlert({
- tips:`“${item.name}”` + this.limitFailStr
- })
- }
- else{
- this.$showAlert({
- tips:`文件大小不能超过${this.limit}MB`
- })
- }
- }, i * 100);
- return;
- }
- } else if (!fileInfo.checkSizeLimit(this.mediaType, item.size)) {
- setTimeout(() => {
- if (this.limitFailStr) {
- this.$showAlert({
- tips:`“${item.name}”` + this.limitFailStr
- })
- }
- else{
- this.$showAlert({
- tips:`上传文件太大,不能超过${fileInfo.mediaMaxSize[this.mediaType]}MB`
- })
- }
-
- }, i * 100);
- return;
- }
- return item
- });
- this.$emit("file-change", this);
- e.target.value = "";
- });
- },
- beforeDestroy() {
- // this.clear();
- },
- methods: {
- click() {
- this.$el.click();
- },
- clear() {
- this.dataURL && window.URL.revokeObjectURL(this.dataURL);
- this.name = null;
- this.type = null;
- this.size = null;
- this.files = null;
- this.dataURL = null;
- this.base64 = null;
- this.poster = null;
- },
- upload() {},
- },
- };
- </script>
|