使用Laravel使用dropzone.js上传多个图像

时间:2017-02-08 09:41:19

标签: php laravel-5 laravel-5.2 laravel-5.3 dropzone.js

我使用带有laravel的dropzone上传多个图像,它可以正常工作。我要求的是我想立即上传它们然后发送电子邮件给我的客户,告诉他照片已经上传。

提前致谢。

<form action="{{route('upload.photos')}}" method="post" enctype="multipart/form-data" class="dropzone" id="dropzoneFileUpload" >
                    {{csrf_field()}}
                    <div>
                        <h3 class="text-center">Upload Multiple Image By Click On Box</h3>
                    </div>
                    <div class="fallback"> 
                        <input name="file" type="file" multiple />
                    </div>


                </form>
<script type="text/javascript">
    Dropzone.options.imageUpload = {
        maxFilesize:500,
        acceptedFiles: ".jpeg,.jpg,.png,.gif",
        autoProcessQueue: false,
        uploadMultiple: true,
        parallelUploads: 25,
        maxFiles: 25

    };
</script>
$file = $request->file('file');    
        $fileName = time().$file->getClientOriginalName();
        Storage::disk('local')->put($car_id.'/'.$fileName, File::get($file));
        Photo::create([
            'photo_name'=>$fileName,
            'car_id'=>$car_id
        ]);
$data = [
        'first_name'=>$car->user->first_name,
        'last_name'=>$car->user->last_name,
        'vin'=>$car->vin,
        'model'=>$car->model,
        'make' =>$car->make
    ];
        Mail::send('admin.email.send_album_notification',$data,function($message) use($car){
            $message->subject('Pictures for Test  - Example ');
            $message->from('noreply@example .com','Example ');
            $message->to($car->user->email,$car->user->full_name);
        });

2 个答案:

答案 0 :(得分:0)

如果我分享我的经验,那么为每个表单创建一个唯一的ID,如

<form>
    <input type="hidden" value="{{ uniqid() .'-'. Carbon\Carbon::now()->timestamp }}" name="form-token"/>
<form>

现在您可以识别每种表格。插入每张图片。跟踪每张图片非常容易。

例如:

Photo::create([
    'photo_name'=>$fileName,
    'car_id'=>$car_id,
    'form_token'=>$request->get('form-token'),
]);

一旦插入,您就拥有form_token列,一次查找所有上传的图像。意思是假设您的用户上传了5张图片,每张图片都有相同的令牌。现在只需通过令牌找到图像并发送到邮件中。

$photos = Photo::whereFormToken($request->get('form-token'))->get();
$data['photos'] = $photos;
Mail::send('admin.email.send_album_notification',$data,function($message) use($car){
   $message->subject('Pictures for Test  - Example ');
   $message->from('noreply@example .com','Example ');
   $message->to($car->user->email,$car->user->full_name);
});

现在,您的邮件视图文件包含照片集合对象,因此您可以打印出来。像这样:

@foreach($photos as $photo_key => $photo_value)
        <?php $pathToImage = public_path()."/".$photo_value->image_name; ?>
        <img src="{!! $photo_value->embed($pathToImage); !!}" style="max-width: 80%;padding-top: 20px;">
@endforeach

答案 1 :(得分:-1)

LOAD DATA