Hashan
Hashan

Reputation: 184

Email multiple images with dropzone laravel

I want to upload multiple image files and send it via email to customers. But the ajax request get this error call to a member function getclientoriginalname() on array when uploadMultiple: true, added to dropzone. Multiple images uploaded without that option. Anyway i want to email that multiple files, how can i do this?

dropzone js code:

Dropzone.options.uploadimg = {
            paramName: "file", // The name that will be used to transfer the file
            maxFilesize: 5, //MB
            acceptedFiles: ".jpeg,.jpg,.png",
            uploadMultiple: true,
            addRemoveLinks: true,
            success: function(file, response) 
            {
                $.notify({
                    message: 'Image uploaded Successfully!' 
                    },
                    {
                    type: 'success'
                });
            },
            error: function(file, response)
            {
               return false;
               console.log('fail to upload');
            },
            headers: {
                'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
            }
        }

SendMailController to send uploaded images.

    public function sendNotifications(Request $request)
    {
        $id_int = Cookie::get('jobid');
        $img_name = Cookie::get('imgName');
        
        $data = DB::table('customers')
        ->join('jobs', 'jobs.id', '=', 'customers.id')
        ->select('firstname','email')
        ->where('jobs.id', '=', $id_int)
        ->get()->toArray();

        foreach ($data as $value) {
            $customer_firstname = $value->firstname;
            $customer_email = $value->email;
        }

        $pathToFile = public_path() . "\\uploads\\" . $img_name;

        //send the email to the relevant customer email
        Mail::to($customer_email)->send(new SendMail($customer_firstname, $pathToFile), function($message){
                $message->attach($pathToFile);
        });

    }

Image upload controller:

class ImageUploadController extends Controller
{
    public function uploadImage(Request $request){

        $img_file = $request->file('file');
        
        $imgName = $img_file->getClientOriginalName();
        
        Cookie::queue(cookie('imgName', $imgName, $minute = 5));

        $img_file->move(public_path('uploads'), $imgName);
    }
}

When i uploaded multiple images and sendinding via email, it only send the last uploaded file in dropzone. How can i send all uploaded files?

Upvotes: 1

Views: 384

Answers (1)

AWS PS
AWS PS

Reputation: 4708

since its multiple files you need to loop through the file variable to get the files

class ImageUploadController extends Controller
{
    public function uploadImage(Request $request){

        $img_files = $request->file('file');
        foreach($img_files as $img_file){
            $imgName = $img_file->getClientOriginalName();

            Cookie::queue(cookie('imgName', $imgName, $minute = 5));

            $img_file->move(public_path('uploads'), $imgName);
        }
    }
}

Upvotes: 1

Related Questions