使用邮递员将文件发送到Laravel API

时间:2015-01-22 16:16:45

标签: php mysql file api laravel

当我尝试将带有邮递员的文件发送到我的Laravel api时,结果总是返回“NULL”

我的控制器看起来像这样:

public function store()
{
    $file = Input::file('image');
    var_dump($file);


}

我的路线档案:

Route::post('test', 'TestController@store');

但是当我尝试在邮递员中发送图像时,我得到结果“NULL”

我的邮递员配置是:

http://app.dev/test(POST)

内容类型 - multipart / form-data

表单数据

image - test.jpg

我错过了什么吗?

我已经检查了php.ini,以便我有足够的空间上传

我的问题与这篇文章中的问题完全一样: Correct Postman Settings when testing file uploading in Laravel 4?

但他的解决方案没有用。我已经检查了我的php.ini

如果我使用资源控制器并从浏览器输入网址,则代码如下:

public function store()
{
    $input = Input::all();

    $path = public_path() .'/images/123/';

    $inpusqt = Input::file('images');

    $i = 1;
    File::exists($path) or File::makeDirectory($path);

    foreach($inpusqt as $file) {
        $filExt = $file->getClientOriginalExtension();
        $ext = '.' . $filExt;

        $lol = Image::make($file->getRealPath());

        $lol->heighten(258)->save($path . $i . $ext);
        $i++; //increment value to give each image a uniqe name
    }
}

但是,如果我将路线修改为例如post::( controller @ store)并使用邮递员发送图像,则会收到错误消息“未定义索引:图像”

5 个答案:

答案 0 :(得分:38)

如果您想使用Postman和Laravel测试文件上传,只需删除Postman中的Content-Type标题设置。

答案 1 :(得分:0)

在PostMan中选择“表单数据”时,它会自动设置标题“ Content-Type:application / x-www-form-urlencoded”。

我刚刚删除了标题,一切正常:)

答案 2 :(得分:0)

如果您发送上载邮递员表格文件的请求,则应在邮递员中包含标题Content-Type:multipart / form-data并选择form-data,该字段应为file而不是text。另外请注意,只有使用POST时,才可以使用PUT和PATCH进行文件上传,而上传不起作用。

答案 3 :(得分:0)

可能的答案 Could not submit form-data through postman put request

标题:Content-Type应用程序/ x-www-form-urlencoded

return $request->file('avatar');

图片示例

enter image description here

答案 4 :(得分:0)

我刚刚删除了标题字段并为我工作