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
Laravel api - how to upload file
提问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 passport
package. In developing environment I created a function store
as follows
我正在使用 Laravel 为移动应用程序构建 Web 服务。我需要让用户上传图片。我正在使用passport
包。在开发环境中,我创建了一个函数store
如下
public function store(Request $request)
{
return $request;
}
and using Firefox poster I post contents. These are the parameters
并使用 Firefox 海报发布内容。这些是参数
The problem is the function store
always 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 邮递员。我删除了所有标题并为文件设置了一个键。如下
it is header
它是标题
and this is the code
这是代码
public function store(Request $request)
{
$file = $request->file('image');
return $file->getClientOriginalExtension();
}
回答by Paras
Returning $request
will not work. If you want to check if the file exists, try return $request->allFiles()
返回$request
将不起作用。如果要检查文件是否存在,请尝试返回$request->allFiles()