ali dehqani
ali dehqani

Reputation: 184

postman doesn't send files to server (laravel+postman)

I send my files to the server using POSTMAN. But on the server side I get this output! enter image description here

This means that my files are not being sent! Why?

this is my upload controller :

   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']);
    }

Upvotes: 0

Views: 1838

Answers (2)

Tohid Dadashnezhad
Tohid Dadashnezhad

Reputation: 1948

The name of the file field in Postman is files however as I noticed in your script that you want to access file. Aslo if you want to send an array of files via Postman to your server, you must add a file in post Postman like this files[].

Upvotes: 2

ali dehqani
ali dehqani

Reputation: 184

i changed my filename from file to file[] and problem resolved

Upvotes: 0

Related Questions