【问题标题】:postman doesn't send files to server (laravel+postman)邮递员不向服务器发送文件(laravel+postman)
【发布时间】:2020-07-11 17:26:00
【问题描述】:

我使用 POSTMAN 将文件发送到服务器。 但是在服务器端我得到了这个输出!

这意味着我的文件没有被发送!为什么?

这是我的上传控制器:

   public function store(Request $request)
    {
        request()->validate([
            'file' => 'required',
            'file.*' => 'mimes:doc,pdf,docx,txt,zip'
        ]);
        // دریافت دایرکتوری مطالبه مربوطه :  $demand=Demand::find(72)->files->first()->file_directoryس
        //{"title":"this is test title","demandContent":"this is test content "} send as form-data request
        $request->data=json_decode($request->data); //دریافت به صورت جیسون و تبدیل به شی
        $demand=new Demand(['title' => $request->data->title,'content'=>$request->data->demandContent,'user_id'=>auth('api')->user()->id]);
        if($demand->save()) //اگر درخواست در دیتابیس قبت شد
        {
            //----------------------------File Upload Scope---------------------------------------
            if($request->hasfile('file'))
            {
                $path='demands/'.$demand->id.'/files';
                foreach($request->file('file') as $file)
                {
                    $filename=$file->getClientOriginalName();
                    $file->move($path, $filename);
                }
                $demand->files()->save(new File(['file_directory'=>$path]));
            }
            //----------------------------File Upload Scope---------------------------------------
            return response()->json(['demand'=>new DemandResource($demand)],200);
        }
        return response()->json(['state'=>'false']);
    }

【问题讨论】:

  • 服务器端代码在哪里?
  • 试试这个:stackoverflow.com/a/59417670/12344897 我们需要更多信息,请上传您的代码
  • @Chalan 好的,谢谢,我上传了我的代码
  • 我认为@TohidDadashnezhad 应该在这里添加他们的评论作为接受的答案 - 它基本上告诉你要改变什么。 :)
  • 是的。我添加了我的评论作为答案,如果您投票,我将不胜感激。谢谢。

标签: laravel file-upload postman


【解决方案1】:

我将文件名从文件更改为文件[] 问题解决了

【讨论】:

    【解决方案2】:

    Postman 中文件字段的名称是 files,但正如我在您的脚本中注意到的那样,您想要访问 file。此外,如果您想通过 Postman 将一组文件发送到您的服务器,您必须在 post Postman 中添加一个文件,例如 files[]

    【讨论】:

      猜你喜欢
      • 2014-08-12
      • 2015-03-21
      • 2017-05-31
      • 2017-07-08
      • 1970-01-01
      • 2019-01-20
      • 2015-03-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多