如何将音频文件从Vue.js上传到Laravel?

时间:2019-02-04 04:17:36

标签: laravel vue.js

我尝试上传音频文件(如图像),但是没有用。如何从音频文件中提取数据并将其发送到Laravel?

<input
type="file"
class="user-edit-form browse-file"
accept=".mp3,audio/*"
@change="uploadSong"
>

uploadSong (e) {
      console.log(e)
      this.songUpload = e.target.files[0]
      if (!this.songUpload.length) {
        return
      }
      this.createSong(this.songUpload[0])
    },
createSong (file) {
      let reader = new FileReader()
      let vm = this
      reader.onload = (e) => {
        vm.song = e.target.result
        console.log(e.target.result)
      }
      reader.readAsDataURL(file)
    }

2 个答案:

答案 0 :(得分:1)

输入:

<input type="file" id="inputName" name="inputName"  v-on:change="fileData(this)" />

在Chnage上

fileData(element){
   var filedata = $(element).prop("files")[0];
   var fileName = file_data.name;
   var fileExtension = fileName.split('.').pop().toLowerCase();
}

答案 1 :(得分:0)

您可以尝试以下代码:

输入:

<input type="file" id="file" ref="file" v-on:change="handleFileUpload()"/>

关于文件更改:

handleFileUpload(){
    this.file = this.$refs.file.files[0];
}

最后在表单上提交:

    submitAudioFile(){

            let formData = new FormData();

            // add file

            formData.append('file', this.file);

            axios.post( '/upload',
                formData,
                {
                headers: {
                    'Accept': 'application/json',
                    'Content-Type': 'multipart/form-data'
                }
              }
            )
        .then(function(){
           console.log('success');
        })
        .catch(function(){
           console.log('failed');
        });