How to get image file data to controller using jquery ajax laravel 5.4

Posted 1 month ago by jignesh1948

I am using Laravel 5.4 and i want to upload file or image using ajax but it's give me an error.

in javascript

var file_data = $("#upload-book-img")[0].files[0];   
var form_data = new FormData();     
form_data.append("file", file_data) // Appending parameter named file with properties of file_field to form_data
form_data.append("ecat_id", ecat_id) // Adding extra parameters to form_data
form_data.append("lang_id", lang_id) // Adding extra parameters to form_data
form_data.append("post_content", post_content) // Adding extra parameters to form_data              
form_data.append("_token", _token) // Adding extra parameters to form_data              


In php controller
public function insertNewPost(Request $request)
{
    $data = $request->file; 
    
    $image_type = $request->file->getClientOriginalExtension(); 
    
    $profilePath = env('EBITES_UPLOAD_IMAGE');
    $img = $this->createunique_image_id('1').'.'. $image_type;
    $path = $profilePath.$img;
    Image::make($data)->save($path);   
    
    
    dd($path);
}                               

Please sign in or create an account to participate in this conversation.

Laracasts Mascot

Hi, Have We Met Yet?

Did you know that, in addition to the forum, Laracasts includes well over 1000 lessons on modern web development? All for the price of one lunch out per month.

Sign Me Up

Channels

Reply to

Use Markdown with GitHub-flavored code blocks.