I am using Laravel as an API and AngularJS as the front-end to interact with it. I am trying to upload a file using ng-file-upload but I am facing a weird issue.
The global variable $_FILES
only contains the uploaded while $request->file('img')
is empty so the custom request validation fails as it can not see the uploaded file.
I have found couple of answers stating to add use Illuminate\\Http\\UploadedFile;
to the controller so I can access the uploaded file but I can't so far. I have checked the code having enctype="multipart/form-data"
in the form but still nothing works
How can I access the file and at the same time the validation rules are applied correctly?
Angular Form:
<div class="small-6 medium-3 columns small-centered">
<form name="products.storeForm" ng-submit="products.store()" enctype="multipart/form-data">
<label>Name</label>
<input type="text" ng-model="products.product.name" name="name" required>
<label>Image</label>
<img ng-src="{{image_source}}">
<input type="file" name="img" onchange="angular.element(this).scope().setFile(this)" ngf-pattern="image/*" accept="image/*" ng-model="products.product.img">
<button type="submit" class="button">Submit</button>
</form>
</div>
Angular Service:
service.store = function(product){
console.log(product.img)
return Upload.upload({
url: url,
headers: { 'Content-Type' : 'application/json' },
method: 'POST',
sendFieldAs: 'form',
fields: {
name: product.name
},
file: product.img,
fileFormDataName: 'image'
})
};
Don't have the means to test this, but I the fact that you are passing the image with the image
key would be my starting point (on your Angular upload service):
fileFormDataName: 'image'
But you are trying to fetch it using the img
key in Laravel:
$request->file('img')
Just try below test code at laravel controller method before $request->file('img')
and I hope you will get the your answer.
print('<pre style="color:red;">');
print_r($request->all());
print('</pre>');
exit;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.