【发布时间】:2017-05-18 21:53:04
【问题描述】:
我使用 Laravel 作为 API,使用 AngularJS 作为前端与之交互。我正在尝试使用 ng-file-upload 上传文件,但我遇到了一个奇怪的问题。
全局变量$_FILES 只包含上传的内容,而$request->file('img') 为空,因此自定义请求验证失败,因为它看不到上传的文件。
我找到了几个答案,说明将use Illuminate\Http\UploadedFile; 添加到控制器,这样我就可以访问上传的文件,但到目前为止我还不能。我检查了表单中包含enctype="multipart/form-data" 的代码,但仍然没有任何效果
如何访问文件并同时正确应用验证规则?
角度形式:
<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>
角度服务:
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'
})
};
【问题讨论】:
-
您是否尝试过将调试模式设置为 true?可能有错误什么的。
-
已经设置为
true,完全没有错误 -
你能显示你的表单和控制器代码吗?
标签: php angularjs laravel file-upload