使用 dropzone laravel 通过电子邮件发送多张图片

Email multiple images with dropzone laravel

我想上传多个图像文件并通过电子邮件发送给客户。但是当 uploadMultiple: true, 添加到 dropzone 时,ajax 请求得到这个错误 call to a member function getclientoriginalname() on array。未使用该选项上传多张图片。不管怎样,我想通过电子邮件发送多个文件,我该怎么做?

dropzone js代码:

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 发送上传的图片。

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

    }

图片上传控制器:

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

当我上传多张图片并通过电子邮件发送时,它只会发送最后上传到 dropzone 中的文件。我怎样才能发送所有上传的文件?

因为它有多个文件,你需要遍历文件变量来获取文件

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