Hello All,
I am building a photo uploader with Vue and Laravel. I seem to have run into a problem I can't figure out. Laravel 5.3 and Vue 1.0+. Here's the current workflow.
- User clicks the button that simulates a "click" action on a hidden form input for file
- The input file has a v-on:change to call a function to pass the file to my photo component
- The Photo component uses Vue Resources to post the file to my app
- Laravel gets the post, evaulates and processes and returns a response
This seems pretty simple, the only problem I am getting is that Laravel can't see the file. Vue resource see's it fine because I can successfully get the "Progress" event so I can display a progress bar of the upload. However when I get in Laravel and try to look at files, it shows no files.
Here's the code
Photo.vue
upload: function(){
// upload the file to the server
var form = new FormData();
form.append('photo', this.file, this.file.name);
this.$http.post(this.url, form, {
progress: function(progress){
this.uploadProgress = Math.floor((progress.loaded / progress.total) * 100);
}.bind(this)
}).then(function(r){
// Completed Successfully
response = JSON.parse(r.body);
if( !response.success ) {
// The file could not be uploaded, so dispatch an error and remove this element.
console.log(response.msg);
return;
}
// If success, map out the response to the local object
this.url = response.url;
this.thumb = response.thumb;
this.isUploading = false;
}.bind(this), function(r){
// Completed with Errors
console.log(r)
}.bind(this))
}
PhotoUploadController.php
public function uploadPhoto($attachToType, $attachToId, Request $request)
{
$allowedExtensions = ['jpg', 'jpeg', 'png', 'gif', 'tiff'];
if( $attachToType == 'property' ) $attachToType = '\ExpressRentals\Models\ScreeningProperty';
if( $attachToType == 'listing' ) $attachToType - '\ExpressRentals\Models\Listing';
// Validate that the file has been uploaded
if( !$request->hasFile('photo')) {
return response(['success' => 0, 'msg' => $request->hasFile('File')]);
}
// Get the file and check that it's allowed
$photo = $request->file('photo');
if(! in_array(strtolower($photo->extension()), $allowedExtensions) ) {
return response(['success' => 0, 'msg' => 'File Format Not Supported']);
}
// Check to make sure the file is larger than 400 pixels wide
if( false ) {
return response(['success' => 0, 'msg' => 'Image must be larger than 400px wide']);
}
// All is good, save the photo to amazon, create the photo object, then return the photo object for display.
return response($photo->extension());
}
Any ideas?
UPDATE
I tried re-writing the function in JQuery, and the same thing is happening. I also was able to see the correct Content-Type of "multipart/form-data" was being received by Laravel. I am using Valet on macOS Sierra, so maybe there is something to do with that?