用邮递员发送文件到 Laravel API
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28093586/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Send file with postman to Laravel API
提问by user2722667
When I try to send a file with postman to my Laravel api the result always returns "NULL"
当我尝试将带有邮递员的文件发送到我的 Laravel api 时,结果总是返回“NULL”
my controller look like this:
我的控制器看起来像这样:
public function store()
{
$file = Input::file('image');
var_dump($file);
}
And my route file:
还有我的路线文件:
Route::post('test', 'TestController@store');
But when I try to send a image in postman I get the result "NULL"
但是当我尝试在邮递员中发送图像时,我得到结果“NULL”
My postman config is:
我的邮递员配置是:
http://app.dev/test(POST)
http://app.dev/test(POST)
Content-Type - multipart/form-data
内容类型 - 多部分/表单数据
form-data
表单数据
image - test.jpg
图像 - test.jpg
Am I missing anything?
我错过了什么吗?
I've checked php.ini so that I have enough space for uploads
我检查了 php.ini 以便我有足够的空间上传
My problem is exactly like the one in this post: Correct Postman Settings when testing file uploading in Laravel 4?
我的问题与这篇文章中的问题完全一样: 在 Laravel 4 中测试文件上传时是否正确邮递员设置?
But his solution didnt work. I have already checked my php.ini
但他的解决方案没有奏效。我已经检查过我的 php.ini
The code bellow works if I use a resource controller and enter the url form a browser:
如果我使用资源控制器并从浏览器输入 url,则下面的代码有效:
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
}
}
But if I modify my route to eg post::(controller@store) and send the images with postman it get an error saying that "Undefined index: images"
但是,如果我将路由修改为例如 post::(controller@store) 并通过邮递员发送图像,则会收到一条错误消息,指出“未定义索引:图像”
回答by MrSnoozles
If you want to test file uploads with Postman and Laravel, simply remove the Content-Type
header setting in Postman.
如果您想使用 Postman 和 Laravel 测试文件上传,只需删除Content-Type
Postman中的标头设置即可。
回答by bruno
If you send request to upload a file form Postman you should include in the postman also a header Content-Type: multipart/form-data and select form-data, there the field should be file and not text . Also be careful only with POST you can make file upload with PUT and PATCH it doesn't function.
如果您发送请求以邮递员形式上传文件,您还应该在邮递员中包含一个标题 Content-Type: multipart/form-data 并选择 form-data,该字段应该是 file 而不是 text 。也只注意 POST 你可以使用 PUT 和 PATCH 上传文件,但它不起作用。
回答by ztvmark
Possible Answer Could not submit form-data through postman put request
可能的答案 无法通过邮递员提交请求提交表单数据
Headers: Content-Type application/x-www-form-urlencoded
标题:内容类型应用程序/x-www-form-urlencoded
return $request->file('avatar');
image example
图片示例
回答by Ashish Kumar
When choosing "form-data" in PostMan, it automatically sets a header "Content-Type: application/x-www-form-urlencoded".
在 PostMan 中选择“form-data”时,它会自动设置一个标题“Content-Type: application/x-www-form-urlencoded”。
I just removed the header, and everything worked :)
我刚刚删除了标题,一切正常:)