如何在Laravel中发送带有文件和数据数组的PUT请求 [英] How to send PUT request with a file and an array of data in Laravel

查看:150
本文介绍了如何在Laravel中发送带有文件和数据数组的PUT请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Laravel作为API和Angularjs作为前端对一个Web应用程序进行编程.我有一个使用PUT方法更新产品的表格,其中包含一系列信息和一个文件作为产品图像.但是我无法在控制器中获得输入请求,它是空的.

I am programing a web app using Laravel as API and Angularjs as frontend. I have a form to update product using PUT method with a array of informations and a file as product image. But I couldn't get the input requests in the controller, it was empty.

请参见下面的代码:

web.php(路由)

web.php ( route )

Route::group(['prefix' => 'api'], function()
{
    Route::put('products/{id}', 'ProductController@update');
});

我的angularjs产品服务:

My angularjs product service :

function update(productId, data, onSuccess, onError){
        var formData = new FormData();
        formData.append('imageFile', data.imageFile);
        formData.append('image', data.image);
        formData.append('name', data.name);
        formData.append('category_id', data.category_id);
        formData.append('price', data.price);
        formData.append('discount', data.discount);
        Restangular.one("/products", productId).withHttpConfig({transformRequest: angular.identity}).customPUT(formData, undefined, undefined,   {'Content-Type': undefined}).then(function(response) {

                onSuccess(response);

            }, function(response){

                onError(response);

            }
        );
    }

我的ProductController更新功能

My ProductController update function

public function update(Request $request, $id) {
// Just print the request data
        dd($request->all());
    }

这就是我在Chrome检查员中看到的

This is what I see in Chrome inspectmen

请分享您在此问题上的经验.谢谢.

Please share your experiences on this problem. Thanks.

推荐答案

您需要的是只有带有新字段_method = put的普通POST请求,您的代码才能正常工作:

what you need is Only normal POST request with new field named _method=put then your code will work normally:

这篇关于如何在Laravel中发送带有文件和数据数组的PUT请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆