You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel+Dropzone多图上传并邮件发送问题求助

解决多图上传并全部通过邮件发送的问题

你的问题核心是开启uploadMultiple: true后,后端接收到的是文件数组,但代码仍按单个文件处理,导致getClientOriginalName()报错;关闭该选项时,虽然能逐个上传,但Cookie只保存最后一个文件名,所以邮件只发最后一张。下面分步骤解决:

第一步:调整Dropzone前端配置

确保paramName明确设置为"file[]"(让后端识别接收的是文件数组),保留uploadMultiple: true:

Dropzone.options.uploadimg = {
  paramName: "file[]", // 明确告知后端接收文件数组
  maxFilesize: 5, // MB
  acceptedFiles: ".jpeg,.jpg,.png",
  uploadMultiple: true,
  addRemoveLinks: true,
  success: function(file, response) {
    $.notify({ message: 'Images uploaded Successfully!' }, { type: 'success' });
  },
  error: function(file, response) {
    console.log('fail to upload', response);
    return false;
  },
  headers: {
    'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
  }
}

第二步:修改ImageUploadController处理多文件上传

后端接收到的$request->file('file')现在是数组,需要循环处理每个文件,同时把所有上传后的文件名用JSON格式存入Cookie(Cookie无法直接存储数组,序列化后再存储):

class ImageUploadController extends Controller {
    public function uploadImage(Request $request){
        $imgFiles = $request->file('file');
        $imgNames = [];
        
        foreach ($imgFiles as $imgFile) {
            $imgName = $imgFile->getClientOriginalName();
            $imgFile->move(public_path('uploads'), $imgName);
            $imgNames[] = $imgName;
        }
        
        // 将文件名数组转为JSON字符串存入Cookie,有效期5分钟
        Cookie::queue(cookie('imgNames', json_encode($imgNames), 5));
    }
}

第三步:修改SendMailController发送所有附件

取出Cookie中的JSON字符串并转回数组,循环添加每个文件作为邮件附件:

public function sendNotifications(Request $request) {
    $id_int = Cookie::get('jobid');
    $imgNamesJson = Cookie::get('imgNames');
    $imgNames = json_decode($imgNamesJson, true); // 转回文件名数组
    
    $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;
    }
    
    // 发送邮件并添加所有附件
    Mail::to($customer_email)->send(new SendMail($customer_firstname), function($message) use ($imgNames) {
        foreach ($imgNames as $imgName) {
            $pathToFile = public_path() . "\\uploads\\" . $imgName;
            $message->attach($pathToFile);
        }
    });
}

第四步:调整SendMail邮件类(可选)

如果你的SendMail类原本依赖单个文件路径参数,可以修改构造函数移除该依赖,让附件逻辑在邮件发送回调中处理:

class SendMail extends Mailable {
    use Queueable, SerializesModels;
    
    public $customer_firstname;
    
    public function __construct($customer_firstname) {
        $this->customer_firstname = $customer_firstname;
    }
    
    public function build() {
        return $this->view('emails.your-mail-template') // 替换为你的实际邮件模板
            ->subject('Your Uploaded Images');
    }
}

关键注意事项

  • 处理多文件时,必须循环遍历文件数组,不能直接对数组调用getClientOriginalName()(此时$request->file('file')是数组而非单个文件对象)
  • Cookie存储数组需先序列化(用JSON),否则会丢失数据
  • 邮件发送时要循环添加所有附件,而非仅添加最后一个文件

这样修改后,就能实现多图上传并全部通过邮件发送的需求了。

内容的提问来源于stack exchange,提问作者Hashan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:04:23