wea*_*ver 6 laravel vue.js axios
我正在尝试使用 vuejs 和 axios 上传多个图像,但在服务器端我得到了空对象。我在标题中添加了 multipart/form-data 但仍然是空对象。
submitFiles() {
/*
Initialize the form data
*/
let formData = new FormData();
/*
Iteate over any file sent over appending the files
to the form data.
*/
for( var i = 0; i < this.files.length; i++ ){
let file = this.files[i];
console.log(file);
formData.append('files[' + i + ']', file);
}
/*`enter code here`
Make the request to the POST /file-drag-drop URL
*/
axios.post( '/fileupload',
formData,
{
headers: {
'Content-Type': 'multipart/form-data'
},
}
).then(function(){
})
.catch(function(){
});
},
Run Code Online (Sandbox Code Playgroud)
HTML:
<form method="post" action="#" id="" enctype="multipart/form-data">
<div class="form-group files text-center" ref="fileform">
<input type="file" multiple="multiple">
<span id='val'></span>
<a class="btn" @click="submitFiles()" id='button'>Upload Photo</a>
<h6>DRAG & DROP FILE HERE</h6>
</div>
Run Code Online (Sandbox Code Playgroud)
我的服务器端代码:
class FileSettingsController extends Controller
{
public function upload(Request $request){
return $request->all();
}
}
Run Code Online (Sandbox Code Playgroud)
输出:
{files: [{}]}
files: [{}]
0: {}
Run Code Online (Sandbox Code Playgroud)
Console.log() 结果:
File(2838972) {name: "540340.jpg", lastModified: 1525262356769, lastModifiedDate: Wed May 02 2018 17:29:16 GMT+0530 (India Standard Time), webkitRelativePath: "", size: 2838972, …}
Dan*_*iel 19
你忘了使用$refs. 添加ref到您的输入:
<input type="file" ref="file" multiple="multiple">
Run Code Online (Sandbox Code Playgroud)
接下来,像这样访问您的文件:
submitFiles() {
let formData = new FormData();
for( var i = 0; i < this.$refs.file.files.length; i++ ){
let file = this.$refs.file.files[i];
formData.append('files[' + i + ']', file);
}
axios.post('/fileupload', formData, {
headers: {
'Content-Type': 'multipart/form-data'
},
}
).then(function(){
})
.catch(function(){
});
},
Run Code Online (Sandbox Code Playgroud)
这应该是有效的。
| 归档时间: |
|
| 查看次数: |
25633 次 |
| 最近记录: |