简体   繁体   中英

Input type file is sending data as text

I have the following simple view file:-

<form action="{{route('testupdate')}}" method="POST">
    {{csrf_field() }}
    <input type="file" name="avatar" >
    <input type="submit">
</form>

In Controller file i am suppose to receive a file type input, but i just get the image name as text:-

public function teststore(Request $request){
        dd($request->all());
        //returned array:2 [▼
        // "_token" => "jFPlhBHXP9jLXCJkrlDg62wR8eVaiBJgEnTD8wBZ"
        //"avatar" => "2.png"

       dd($request->has('avatar'));
       // returned true

       dd($request->hasFile('avatar');
       //returned null (Although the input type is "file")
  } 

Please advice

Try using

<form action="{{route('testupdate')}}" method="POST" enctype="multipart/form-data">

you need to include tag enctype="multipart/form-data" in your input field. This tag is necessary whenever you are using file uploads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM