I'm trying to upload a file (Excel sheet) from a front-end app build with VueJS to an API build with Laravel 5.5. I've some form request validation which says to me that The file field is required. So the file doesn't get uploaded to my API at all.
VueJS file upload:
onFileChange(e) {
let files = e.target.files || e.dataTransfer.files;
if (files.length <= 0) {
return;
}
this.upload(files[0]);
},
upload(file) {
this.form.file = file;
this.form.put('courses/import')
.then((response) => {
alert('Your upload has been started. This can take some time.');
})
.catch((response) => {
alert('Something went wrong with your upload.');
});
}
this.form is a Form class copied from this project but the data() method returns a FormData object instead of a object.
data() method:
data() {
let data = new FormData();
for (let property in this.originalData) {
data.append(property, this[property]);
}
return data;
}
The route:
FormRequest rules:
public function rules()
{
return [
'file' => 'required',
];
}
If I look to the Network tab in Chrome DevTools it does seem that the request is send correctly: (image after click).
I've tried a lot of things, like sending the excel as base64 to the api. But then I couldn't decode it correctly. So now I'm trying this, but I can't solve the problem.
Edit (controller function)
public function update(ImportRequest $request, CoursesImport $file)
{
$courses = $file->handleImport();
dispatch(new StartCourseUploading($courses, currentUser()));
$file->delete();
return ok();
}


ImportRequest. So the code inside the method never gets executed.