【问题标题】:Laravel and dropzone.js, uploading multiple images replicate last one chosenLaravel 和 dropzone.js,上传多张图片复制最后一张选择
【发布时间】:2018-02-24 17:18:12
【问题描述】:

我正在使用 laravel 和 dropzone.js

当我一张一张上传图片时,效果很好。

但是,当我选择多张图片(例如 3 张图片)时。

它确实保存了图像 3 次,但它保存了最后一张选择的图像,而不是 1、2 和 3。它是 3、3 和 3。

这很奇怪,因为它实际上做了 3 个请求,因此每个图像都应该单独保存。

这是我的表格:

<div class="z-index">
  <button id="modal-open" data-toggle="modal" data-target="#myModalHorizontal">Upload Gallery</button>
</div>
<div class="col-xs-12">
  <div class="modal fade" id="myModalHorizontal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
      <div class="modal-content">
        <!-- Modal Header -->
        <div class="modal-header">
          <button type="button" class="close" data-dismiss="modal" onclick="refreshPage();">
            <span aria-hidden="true">&times;</span>
            <span class="sr-only">Close</span>
          </button>
          <h4 class="modal-title" id="myModalLabel">
            Drag & Drop images or click to upload
          </h4>
        </div>
        <div class="modal-body">
          <form style="height: 50vh;" action="{{ url('/uploadgallery')}}" class="dropzone" id="gallery">
            <input type="hidden" name="_token" value="{{ csrf_token() }}">
          </form>
        </div>
      </div>
    </div>
  </div>
</div>

和控制器:

public function gallery(ImgRequest $request)
{
    if ($request->hasFile('file')) {
        $s3Path = config('app.path', public_path());
        $file = Input::file('file');
        print_r($file);
        $filePath = 'public/gallery/' . time() . '.' . $file->getClientOriginalExtension();
        $path = $s3Path . $filePath;
        Storage::disk('s3')->put($filePath, file_get_contents($file), 'public');
        $session = session()->get('key');
        $image = new Images;
        $image->path = $path;
        $image->entity_id = $session;
        $image->save();
        print_r($path);
    }

【问题讨论】:

    标签: laravel laravel-5 dropzone.js


    【解决方案1】:

    尝试更改此行:$filePath = 'public/gallery/' . time() . '.' . $file-&gt;getClientOriginalExtension();

    $filePath = 'public/gallery/' . $file->getClientOriginalName();
    

    原因是:文件都是同时上传的,time()就是把所有的图片都命名为同名,到了服务器上当然只有一张图片,因为你不能有多个具有相同名称的图像。

    如果您实际上尝试使用您的代码使用 2 个不同的文件扩展名,例如 .jpg.png 文件,它们都会被上传。

    希望有所帮助。

    【讨论】:

      【解决方案2】:

      通常 dropzone.js 使用 ajax 上传图片。我可以看看你的 js 函数来初始化 dropzone 吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-03-23
        • 2021-12-01
        • 2019-09-05
        • 2018-04-28
        相关资源
        最近更新 更多