文件上传删除服务器中上传的文件大小,el-upload 上传文件到服务器,上传之前在before-upload中异步判断文件后缀,宽高分辨率,size大小...

需要在文件上传到obs之前判断视频的分辨率 宽高比例

但是直接从file对象中只能拿到文件的后缀格式,体积大小;所以需要借助video来获取资源的宽高

通过video来获取视频的宽高 需要等到视频加载完,监听loadedmetadata事件;此处需要用到异步

查看 el-upload中before-upload说明,若返回 false 或者返回 Promise 且被 reject,则停止上传;所以在before-upload的回调方法beforeAvatarUpload中返回promise,如果验证不通过则reject停止上传,并且返回reject会自动触发on-remove方法来移除文件videoResolution:[{width: 1920, height: 1080}, {width: 544, height: 960}]

// 手动清除表单校验验证信息

validateField(formName, type) {

this.$refs[formName].validateField(type);

},

videoUpload.vue

class="upload-demo"

v-loading="loading"

action="#"

:on-remove="handleRemove"

:before-upload="beforeAvatarUpload"

:limit="ruleData.limit"

:on-exceed="handleExceed"

:http-request="httpRequest"

>

点击上传

import { Upload } from '@x-tech/fe-puma-lib-upload';

export default {

props: {

value: {

type: Array,

},

ruleData: {

type: Object,

default: () => {

return {

type: 'mp4',

size: 50,

limit: 1

};

}

},

resolution: {

type: Array,

default: ()=> {

return [{

width: 1920,

height: 1080

}];

}

}

},

data() {

return {

dialogImageUrl: '',

dialogVisible: false,

imgObjList: [],

uploadImgList: this.value,

loading: false,

};

},

methods: {

// mp3 or mp4

handleRemove(file, fileList) {

console.log('handleRemove', file, fileList, this.imgObjList);

this.imgObjList = this.imgObjList.filter(item => item.file.uid !== file.uid);

console.log(this.imgObjList);

this.$emit('input', this.getImgObsUrl(this.imgObjList));

},

handleExceed(files, fileList) {

console.log(files, fileList, this.imgObjList);

if (this.imgObjList.length === Number(this.ruleData.limit)) {

this.$message.warning('当前限制选择 1 个文件');

}

},

beforeAvatarUpload(file) {

console.log('beforeAvatarUpload',file);

console.log(this.resolution);

return new Promise((resolve, reject) => {

// 判断size width height 是否上传符合规范

let typeLists = this.ruleData.type.map(item => item.toLowerCase());

const isVideo = !!typeLists.find(item => file.type.includes(item));

const isLt20M = file.size / 1024 / 1024 < this.ruleData.size;

if (!isVideo) {

this.$message.error(`上传视频只能是 ${typeLists} 格式!`);

reject();

}

if (!isLt20M) {

this.$message.error(`上传视频大小不能超过 ${this.ruleData.size}MB!`);

reject();

}

let videoElement = document.createElement('video');

console.log('videoElement', videoElement);

console.dir(videoElement);

videoElement.addEventListener('loadedmetadata', ()=> {

console.dir(videoElement);

const { videoWidth, videoHeight, duration } = videoElement;

console.log( videoWidth, videoHeight );

let isResolution = !!this.resolution.find(item => {

return Number(videoWidth) === Number(item.width) && Number(videoHeight) === Number(item.height);

});

// isResolution = Number(videoWidth) === Number(this.resolution.width) && Number(videoHeight) === Number(this.resolution.height);

console.log(isResolution, 'isResolution');

if (!isResolution) {

this.$message.error('上传图片分辨率不符合要求!');

reject();

}

resolve();

});

let url= URL.createObjectURL(file);

videoElement.src = url;

// this.$refs.video.src = url;

});

// return true;

},

httpRequest(data) {

this.loading= true;

console.log('httpRequest', data, data.file);

// 操作上传到obs 师 封装的upload方法调用

this.uploadExcel(data.file);

},

async uploadExcel(file) {

let upl;

if (process.env.VUE_APP_ENV === 'prod') {

upl = Upload.createProdUpload();

} else {

upl = Upload.createDevUpload();

}

// await返回是临时地址,需要后台进行转存

let url = await upl.upload(file, false);

this.imgObjList.push({url: url, file: file});

console.log(this.imgObjList);

this.$message.success('上传成功');

console.log(this.getImgObsUrl(this.imgObjList));

this.$emit('input', this.getImgObsUrl(this.imgObjList));

this.loading = false;

},

getImgObsUrl(list) {

return list.map(item => item.url);

},

},

};

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在`el-upload`,可以利用`before-upload`和`file-list`两个属性来判断是否上文件。 1. `before-upload`属性:它是一个函数,会在上文件之前被调用,你可以在这个函数进行一些判断逻辑,如果返回 false,那么上流程将停止。在这个函数,可以通过 `file` 参数获取当前要上文件的相关信息,例如文件名、文件大小等。因此,我们可以在这个函数判断是否有文件要上,例如: ``` <el-upload action="/upload" :before-upload="handleBeforeUpload" :file-list="fileList" > <el-button>上文件</el-button> </el-upload> <script> export default { data() { return { fileList: [] } }, methods: { handleBeforeUpload(file) { if (file) { // 有文件要上 return true } else { // 没有文件要上 this.$message.warning('请选择文件') return false } } } } </script> ``` 2. `file-list`属性:它是一个数组,用来存储已经上文件信息。我们可以在该数组判断是否有文件已经上,例如: ``` <el-upload action="/upload" :file-list="fileList" > <el-button>上文件</el-button> </el-upload> <script> export default { data() { return { fileList: [] } }, methods: { handleUploadSuccess(response, file, fileList) { this.fileList = fileList }, handleUploadError(error, file, fileList) { this.fileList = fileList } } } </script> ``` 在上面的代码,我们定义了`handleUploadSuccess`和`handleUploadError`两个事件处理函数,用来处理上成功和上失败的情况。在这两个函数,我们都会更新`fileList`数组,所以我们可以在`fileList`数组的长度上判断是否有文件已经上,例如: ``` if (this.fileList.length > 0) { // 有文件已经上 } else { // 没有文件已经上 } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值