axios 如何上传视频文件?

<h4>上传老师介绍视频</h4>
            <div class="teacher-avatar">
                <!-- <IntroduceTeacherVideo/> -->
                <Button type="primary" icon="ios-search">
                    <label class="btn" for="teacherIntroductionVideo">上传老师介绍视频</label>
                </Button>
                <input type="file" 
                    id="teacherIntroductionVideo" 
                    style="position:absolute; clip:rect(0 0 0 0);" 
                    accept="audio/mp4, video/mp4" 
                    @change="uploadTeacherVideo($event)">
            </div>
uploadTeacherVideo(e){
            var file = e.target.files[0]
            if (!/\.(mp4|avi)$/.test(e.target.value)) {
                this.$Message.error({
                    content:'视频类型必须是.mp4、.avi中的一种',
                    duration:3
                });
                return false
            }
            let data = window.URL.createObjectURL(new Blob([e.target.result]))

            let formdata = new FormData();
            formdata.append('imgStream',data);
            console.log('正在上传视频。。。')
            this.uploadFileToQiniu(formdata);
        },
        uploadFileToQiniu(formdata){
            this.$post(`${this.$url}/teacher/uploadFileToQiniu`,formdata)
            .then(res=>{
                if(res.data.success){
                    let hashKey = res.data.data;
                    this.avatarSrc = `${this.$qiniuImgUrl}/${hashKey.key}`;
                    this.$Message.success({
                        content:'上传成功!',
                        duration:3
                    });
                }else{
                    this.$Message.error({
                        content:res.data.msg,
                        duration:3
                    });
                }
            })
            .catch(err=>{
            })
        

这样写好像报错
clipboard.png

阅读 4.1k
1 个回答

改成这样就OK了

uploadTeacherVideo(e){
            var file = e.target.files[0]
            if (!/\.(mp4|avi)$/.test(e.target.value)) {
                this.$Message.error({
                    content:'视频类型必须是.mp4、.avi中的一种',
                    duration:3
                });
                return false
            }
            let data = e.target.result;

            let formdata = new FormData();
            formdata.append('imgStream',file);
            console.log('正在上传视频。。。')
            this.uploadFileToQiniu(formdata);
        },
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进