2017-03-20 67 views
4

我需要通過Axios發送文件從客戶端到服務器。如何通過Axios發送文件到Laravel

這裏是我的Vuejs代碼:

methods: { 
    'successUpload': function (file) { 
     const config = { headers: { 'Content-Type': 'multipart/form-data' } }; 
     axios.post('/Upload/File',file, config).then(function (response) { 
      console.log(response.data); 
     }); 
    } 
} 

這裏是我的處理代碼Laravel發送文件:

但它總是返回No It's not a File

任何幫助將不勝感激。

+0

你能告訴你如何調用'successUpload'嗎? –

+0

我正在使用Dropzonejs上傳文件 –

+1

僅供參考,但如果您使用的是dropzone,則不需要使用axios來上傳文件。 –

回答

11

您必須創建一個FormData對象並追加圖像文件。

methods: { 
    'successUpload': function (file) { 

    let data = new FormData(); 
    data.append('file', document.getElementById('file').files[0]); 

    axios.post('/Upload/File',data).then(function (response) { 
     console.log(response.data); 
    }); 
    } 
} 

一個例子是here

讓我知道這是否有效。

+1

是的,它像一個魅力工作! –