AngularJS的put方法上传文件没有数据 [英] AngularJS put method to upload files gives no data
问题描述
我有一个
问题是为什么在 PUT
而不是 POST
时会发生这种情况,我该如何解决?我也可以使用 POST
来更新文件,但这是一个我想避免的棘手的解决方案.
存在类似的问题,但没有适当的解决方案可以在使用 PUT
时解决此问题:
进一步研究之后,这似乎是PHP的核心问题,而不是AngularJS或Symfony中的错误.
问题是PHP PUT和PATCH无法解析请求,因此内容根本不可用.(您可以在此处了解更多信息: https://bugs.php.net/bug.php?id = 55815 )
一种解决方法是使用 POST
方法,但将方法伪装成 PATCH/PUT
的方法,如下所示:
$ scope.fileUpload = function(file){var fd = new FormData();fd.append("title",file.title);fd.append("file",$ scope.file);fd.append('_ method','PUT');$ http.post('example/update',fd,{transformRequest:angular.identity,标头:{'Content-Type':未定义}}).然后({//做一点事});};
I have a Symfony application that uses AngularJS on the front-end to upload files with ajax via the POST
method.
Working POST Method
The data is added as FormData
and some angular.identity
magic is used to auto populate the correct application/x-www-form-urlencoded; charset=UTF-8
content-type:
$scope.fileUpload = function (file) {
var fd = new FormData();
fd.append("title", file.title);
fd.append("file", $scope.file);
$http.post('example/upload', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then({
// do something
});
};
This works as expected allowing me to access the posted variables in my controller:
// Expects data from a post method
public function postFile(Request $request)
{
$title = $request->get('title');
/** @var $file UploadedFile */
$file = $request->files->get('file');
// data success, all is good
}
Failing PUT Method
However when I do exactly the same using the PUT
method, I get a 200
success but there is no accessible data:
$scope.fileUpload = function (file) {
var fd = new FormData();
fd.append("title", file.title);
fd.append("file", $scope.file);
$http.put('example/update', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then({
// do something
});
};
// Expects data from a put method
public function putFile(Request $request)
{
$title = $request->get('title');
/** @var $file UploadedFile */
$file = $request->files->get('file');
// the request parameters and query are empty, there is no accessible data
}
The question is why does this occur with PUT
but not POST
and how can I get around this? I could use POST
to update the file too but that's a hacky solution I'd like to avoid.
There are similar questions but no appropriate solutions that fix the issue whilst using PUT
:
Send file using PUT method Angularjs
AngularJS image upload with PUT, possible, how?
Angularjs file upload in put method not working
After delving further into this it appears this is a core PHP issue rather than a bug in AngularJS or Symfony.
The issue is that PHP PUT and PATCH do not parse the request so the content simply isn't available. (You can read more here: https://bugs.php.net/bug.php?id=55815)
A workaround to this is using the POST
method but spoofing the method to be that of PATCH/PUT
like so:
$scope.fileUpload = function (file) {
var fd = new FormData();
fd.append("title", file.title);
fd.append("file", $scope.file);
fd.append('_method', 'PUT');
$http.post('example/update', fd, {
transformRequest: angular.identity,
headers: {'Content-Type': undefined}
}).then({
// do something
});
};
这篇关于AngularJS的put方法上传文件没有数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!