axios上传视频或音频时后端接口405

最近在做一个后台管理项目,视频上传到腾讯云点播,音频上传到对象存储。这就需要区分是视频或音频了。

当上传音频时调用后端给的接口,上传到对象存储。

这里遇到的问题主要是自己对axios不太熟练,导致后台接口返回405,

HTTP Status 405 – Method Not Allowed


Type Status Report

Message Request method 'GET' not supported

Description The method received in the request-line is known by the origin server but not supported by the target resource.


Apache Tomcat/8.0.0

如下图所示:

前端代码如下:

<form ref="vExample" method="post" enctype="multipart/form-data">
        <button
          ref="video_btn"
          type="button"
          :class="(uploaderPercent>0  && uploaderPercent<100) ? 'video_btn_gray' : 'video_btn'"
          @click="vExampleAdd"
          :disabled="(uploaderPercent>0  && uploaderPercent<100)"
        >开始上传</button>
        <input
          type="file"
          name="upfile"
          style="display:none;"
          accept="audio/mpeg, audio/mp3, video/mp4"
          ref="vExampleFile"
          @change="vExampleUpload"
        >
</form>

vuejs代码如下:

vExampleAdd: function () {
      this.$refs.vExampleFile.click()
    },
    /**
     * vExample示例。上传视频过程。
     **/
    vExampleUpload: function () {
      let self = this
      // 获取上传实例
      let videoFile = this.$refs.vExampleFile.files[0]
      let index = ''
      for (let key in UE.instants) {
        index = key
      }
      if (videoFile.type.indexOf("audio") !== -1) {

        axios.post(`${CFG.API_URL}xsnArticle/uploadUeditorVideo`).then(req => {
          console.log("req:", req.data)
          
        }).catch(err => {
          console.error('Error: ', err)
        })


      } else {
        //这里是视频上传到腾讯云点播的代码, 这里没问题,这篇文章的重点不在这里
        let uploader = this.tcVod.upload({
          videoFile: videoFile
        })

        self.uploaderInfo = {
          videoInfo: uploader.videoInfo,
          isVideoUploadSuccess: false,
          progress: 0,
          cancel: function () {
            uploader.cancel()
          }
        }

        uploader.on('video_progress', function (info) {
          self.uploaderPercent = parseInt(info.percent * 100 + '%')
        })
        uploader.on('video_upload', function (info) {
          self.uploaderInfo.isVideoUploadSuccess = true
        })
        uploader.done().then(function (doneResult) {
          UE.instants[index].execCommand(
            'insertHtml',
            '<img width="300" height="200" id="video_' +
            doneResult.fileId +
            '" _url="' +
            doneResult.video.url +
            '" alt="' +
            doneResult.fileId +
            '" class="edui-upload-video  vjs-default-skin" src="/static/UEditor/themes/default/images/spacer.gif" style ="background:url(/static/UEditor/themes/default/images/videologo.gif) no-repeat center center; border:1px solid gray;" x5-video-player-type="h5" playsinline webkit-playsinline />'
          )
        }).then(function (videoUrl) {
          self.$refs.vExample.reset()

          self.dialogVisible = false
        })
      }

    },

主要是不太了解axios的工作原理,导致调用后端接口时直接就是:

axios.post(`${CFG.API_URL}xsnArticle/uploadUeditorVideo`).then(req => {
          console.log("req:", req.data)
          
        }).catch(err => {
          console.error('Error: ', err)
        })

并没有给后端传参数,也并没有把需上传的文件传给后端接口。所以才导致后端接口返回405。

后来经过google,找到一篇文章完美的解决了我的困惑,修改代码如下:(视频上传的代码这里省略)

let videoFile = this.$refs.vExampleFile.files[0]
      let index = ''
      for (let key in UE.instants) {
        index = key
      }
      if (videoFile.type.indexOf("audio") !== -1) {

        //在这里声明所需上传的文件参数,并把videoFile赋值给audioData
        const audioData = new FormData();
        audioData.append('upfile', videoFile);

        axios.post(`${CFG.API_URL}xsnArticle/uploadUeditorVideo`, audioData).then(req => {
          console.log("req:", req.data)
          
          
        }).then(function () {
          self.$refs.vExample.reset()

          self.dialogVisible = false
        }).catch(err => {
          console.error('Error: ', err)
        })


      }

打印出数据了接口为200.后台也可以获取到上传的音频,并把链接发送给了前端,到此问题得以解决。

总结:当使用axios上传文件时,需把上传的文件当成参数传给后端,表单需这样设置

const audioData = new FormData();

audioData.append('upfile', videoFile);

也有的人说需要设置

'Content-Type': 'multipart/form-data' 

其实并没有必要。

 

有用的链接:

https://github.com/axios/axios/issues/318

https://www.jianshu.com/p/85247afabdea

https://blog.csdn.net/u012049760/article/details/71159800

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值