dou70260
dou70260
2018-07-24 22:13

'multipart / form-data'无效

I'm using Laravel and trying to build a gallery, i'm testing the upload of a file but i when i attach a file and click submit i can't get a positive outcome using the test set up. The code is below

GalleryController

// Store Gallery
    public function store(Request $request){
            // Get Request Input
        $name = $request->input ('name');        
        $description = $request->input ('description', '');
        $cover_image = $request->input ('cover_image');
        $owner_id = 1;

        // Check Image Upload
        if($cover_image){
die ('YES');
        } else {
die ('NO');
        }
    }

The form is set up as follows

{!! Form::open(array('action' => 'GalleryController@store', 'enctype' => 'multipart/form-data')) !!}
              {!! Form::label ('name', 'Name') !!}
              {!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Name', 'name' => 'name']) !!}

            {!! Form::label ('description', 'Description') !!}
              {!! Form::text ('name', $value = null, $attributes = ['placeholder' => 'Gallery Description', 'name' => 'Description']) !!}

            {!! Form::label ('cover_image', 'Cover Image') !!}
            {!! Form::file('cover_image') !!}

            {!! Form::submit ('Submit', $attributes = ['class' => 'button']) !!}
            {!! Form::close() !!}

Any help is appreciated Thanks

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dongtou2016 dongtou2016 3年前

    Your form looks correct, it is likely your controller where you are retrieving the uploaded file.

    As per the docs, to retrieve an uploaded file you should use $request->file():

     $request->file('cover_image');
    

    That link to the docs above also goes on to explain how you can check the file properly and store the file.

    点赞 评论 复制链接分享
  • dongyili5843 dongyili5843 3年前
    //use input facades.
    
    use File;
    use Illuminate\Support\Facades\Input;
    
     public function store(Request $request){
            // Get Request Input
            $name = $request->input ('name');        
            $description = $request->input ('description', '');
            $cover_image = $request->input ('cover_image');
            $owner_id = 1;
    
            // Check Image Upload
            if(Input::hasFile('cover_image'){
               die ('YES');
            }
            else {
               die ('NO');
            }
      }
    
    点赞 评论 复制链接分享

为你推荐