Laravel api - 如何上传文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/52801133/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 18:06:50  来源:igfitidea点击:

Laravel api - how to upload file

laravelpostlaravel-5postman

提问by M a m a D

I am building a web service for mobile application using Laravel. I need to let the users to upload images. I am using passportpackage. In developing environment I created a function storeas follows

我正在使用 Laravel 为移动应用程序构建 Web 服务。我需要让用户上传图片。我正在使用passport包。在开发环境中,我创建了一个函数store如下

public function store(Request $request)
{
    return $request;
}

and using Firefox poster I post contents. These are the parameters

并使用 Firefox 海报发布内容。这些是参数

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

The problem is the function storealways returns empty array while I expect it to return information about the file. What is wrong here?

问题是函数store总是返回空数组,而我希望它返回有关文件的信息。这里有什么问题?

thanks

谢谢

回答by M a m a D

I found it. Firefox Poster has problems with this. I used Chrome Postman. I removed all the headersand set a key for file. as follows

我找到了。Firefox Poster 有这个问题。我使用了 Chrome 邮递员。我删除了所有标题并为文件设置了一个键。如下

enter image description here

在此处输入图片说明

it is header

它是标题

enter image description here

在此处输入图片说明

and this is the code

这是代码

public function store(Request $request)
{
    $file = $request->file('image');
    return $file->getClientOriginalExtension();
}

回答by Paras

Returning $requestwill not work. If you want to check if the file exists, try return $request->allFiles()

返回$request将不起作用。如果要检查文件是否存在,请尝试返回$request->allFiles()