放置方法中的AngularJS文件上传不起作用 [英] Angularjs file upload in put method not working
问题描述
我有一个简单的待办事项应用程序,其中我试图将一张照片和一个待办事项一起上传.现在,我创建了这个工厂函数来处理待办事项的创建
I have a simple todos application in which i am trying to upload a photo along with a single todo. Now i have created this factory function that takes care of the creation of the todo
todosFactory.insertTodo = function(todo){
return $http.post(baseUrl, todo, {
headers: {
'Content-Type' : undefined
},
transformRequest: function(data, headersGetter){
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
return formData;
}
});
};
工厂更新方法如下
todosFactory.updateTodo = function(id, todo){
return $http.put(baseUrl + '/' + id, todo, {
headers: {
'Content-Type' : undefined
},
transformRequest: function(data, headersGetter){
var formData = new FormData();
angular.forEach(data, function (value, key) {
formData.append(key, value);
});
return formData;
}
});
};
insertTodo可以按预期工作,但updateTodo没有将数据发布到服务器.我正在使用laravel作为API,并且更新API端点具有此代码
The insertTodo is working as expected but the updateTodo is not posting data to the server. I am using laravel for the API and the update API endpoint has this code
dd(Input::all(), Input::file('cover_photo'));
这表明注释已发布在请求中.如果我更改了updateTodo工厂方法中的发布权,它将开始工作.我是否缺少一些额外的标题或其他内容?
This shows that noting was posted in the request. And if i change the put to post in the updateTodo factory method it starts working. Am i missing some extra headers or something ?
如果我同时从updateTodo方法中删除了transformRequest
和headers
,则在服务器上成功接收了文本数据,但是文件却没有.
如果我只删除了headers
,则什么也没有被发布
If i remove both the transformRequest
and headers
form the updateTodo method, the text data is successfully received on the server but the the file doesn’t.
And if i just remove the headers
nothing gets posted
我正在使用此命令使用laravel随附的PHP服务器
I am using the PHP server that comes with laravel using this command
php artisan serve
更新
由于@Ricardo的回答,我能够使用此代码获取提交的数据
Update
Thanks to @Ricardo's answer i was able to get the submitted data using this code
router->put('/todos/{id}', function($id){
$stream = file_get_contents("php://input");
echo $stream;
die();
});
我正在以这种格式获取视频流
I am getting the stream in this format
-----------------------------3643739756006088191021064137
Content-Disposition: form-data; name="id"
5
-----------------------------3643739756006088191021064137
Content-Disposition: form-data; name="name"
Image Todo
-----------------------------3643739756006088191021064137
Content-Disposition: form-data; name="complete"
0
-----------------------------3643739756006088191021064137
Content-Disposition: form-data; name="created_at"
2015-10-02 06:28:10
-----------------------------3643739756006088191021064137
Content-Disposition: form-data; name="updated_at"
2015-10-02 06:28:10
-----------------------------3643739756006088191021064137
Content-Disposition: form-data; name="cover_photo"; filename="Dragon-Falls-Venezuela-5.jpg"
Content-Type: image/jpeg
�����JFIF��H�H������Exif��MM�*�����i������������������z���,����UNICODE��C�R�E�A�T�O�R�:�
�g�d�-�j�p�e�g� �v�1�.�0� �(�u�s�i�n�g� �I�J�G� �J�P�E�G� �v�6�2�)�,� �q�u�a�l�i�t�y� �=� �9�0�
现在我需要解析这种原始格式.我发现其他人建议的两种php方法 http://php. net/manual/en/function.mb-parse-str.php 和 http://php.net/manual/zh-CN/function.parse-str.php ,但它们都无法正常工作,并且没有给出$ _GET或$ _POST之类的数组.
Now i need to parse this raw format. I have found two php methods that other people have suggested http://php.net/manual/en/function.mb-parse-str.php and http://php.net/manual/en/function.parse-str.php but both of them are not working properly and do not give an array like $_GET or $_POST.
推荐答案
This piece from the PHP documentation discusses the PUT method and file uploads (multipart/form-data).
我无法引用其中的任何部分,因为我认为一切都很重要.您应该详细阅读它,但要总结一下,不更改Apache配置和创建辅助脚本是不可能的.
I can't quote any parts from it because I think everything is important. You should read it in detail but to summarize it's not possible without changes to your Apache configuration and creation of auxiliary scripts.
使用Laravel,您应该使用表单方法欺骗(基本上是变量_method
),并继续使用POST. _method
将允许您正确调用Laravel的PUT操作.
With Laravel you should use Form Method Spoofing (basically a variable called _method
) and keep on using POST. The _method
will allow you to call Laravel's PUT action correctly.
修改
默认情况下,AngularJS以JSON格式发送数据,这说明了为什么无法使用$_POST
全局变量访问数据.我已经在另一个问题中回答了这个问题,该问题解释了您必须做什么.
AngularJS, by default, sends data in the JSON format which explains why you cannot access the data with the $_POST
global. I have answered this in another question which explains what you have to do.
这篇关于放置方法中的AngularJS文件上传不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!