通过 REST 使用 Laravel 上传文件

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

File upload with Laravel through REST

phplaravelrestapiupload

提问by Bisgaard

I'm trying to upload videos (i have also tried images) through a REST api in Laravel through Postman.

我正在尝试通过 Postman 通过 Laravel 中的 REST api 上传视频(我也尝试过图像)。

So far I have tried to do the simplest upload, but nothing seems to work. I'm doing a POST request to http://localhost:8000/api/videosusing the "form-data" option in PostMan and setting the "file" option so i can use the "browse" button to find a file on my PC and calling the file "file".

到目前为止,我已尝试进行最简单的上传,但似乎没有任何效果。我正在使用 PostMan 中的“form-data”选项向http://localhost:8000/api/videos发出 POST 请求并设置“文件”选项,以便我可以使用“浏览”按钮在我的电脑并调用文件“文件”。

In the controller i'm doing this:

在控制器中,我这样做:

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

But i get the error "Call to a member function getClientOriginalExtension() on null". I have also tried:

但是我收到错误“在 null 上调用成员函数 getClientOriginalExtension()”。我也试过:

return Input::file('file')->getClientOriginalExtension();

But it's the same result.

但结果是一样的。

Anyone have an idea what i'm doing wrong?

任何人都知道我做错了什么?

回答by Bisgaard

Okay, I found out what was wrong. 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 :)

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