【问题标题】:Passing some id from Dropzone using Laravel使用 Laravel 从 Dropzone 传递一些 id
【发布时间】:2017-12-08 02:40:16
【问题描述】:

我有一个模态,可以像这样将图像上传到图库:

<div id="imagesModal" class="modal fade" role="dialog">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
                <h4 class="modal-title"></h4>
            </div>
            <div class="modal-body">
            <form name="imagesForm" id="addImages" class="form-horizontal dropzone" role="form" method="POST" action="{{ route('images.add') }}" enctype="multipart/form-data">
                {{ csrf_field() }}
                {{ method_field('POST') }}
            </form>
            </div>
        </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
</div><!-- /.modal -->

这是我的 jquery 脚本:

$( "#addImages" ).click(function(event) {
                event.preventDefault();
                save_method = 'add';
                $('input[name=_method]').val('POST');
                $('#imagesModal').modal("show");
                $('#imagesForm')[0].reset();
                $('.modal-title').text('Add Images')
            });

            Dropzone.options.addImages = {
                paramName: "images", // The name that will be used to transfer the file
                maxFilesize: 2, // MB
            };

and i already add the route and the function

   public function store(Request $request)
    {
        $gallery = Gallery::find($request->id);
        $path = 'public'.'/'.str_slug($gallery->name);

        foreach ($request->images as $file) {
            $image = new Image();
            $image->gallery_id = $gallery->id;
            $image->name = $file->getClientOriginalName();
            $image->extension = $file->extension();
            $image->size = $file->getClientSize();
            $image->save();

            // Or any custom name as the third argument
            Storage::putFileAs($path, $file, $file->getClientOriginalName());
        }

    }

我的问题是我不知道如何传递画廊 ID,以便将其访问到函数中?我有一些想法将其放在按钮中的 data-gallery-id 上,但是如何传递该数据以便将其与图像一起发送?

【问题讨论】:

    标签: laravel laravel-5 dropzone.js


    【解决方案1】:

    我认为我们不能随图像对象一起发送。但是你可以这样做。您可以创建一种隐藏的输入类型以将图片库 ID 存储在您的表单中,例如: 提交表单后,您可以在控制器中获取 $request->id。

    【讨论】:

      【解决方案2】:

      根据他们的Documentation

      params:可以是要传输到服务器的附加参数的对象,也可以是使用文件、xhr 调用的函数,如果是分块上传,则可以是块参数。如果是函数,这需要返回一个映射。 默认实现对于普通上传什么都不做,但是为分块上传添加相关信息。

      所以你可以修改你的dropzone配置

      Dropzone.options.addImages = {
          paramName: "images", // The name that will be used to transfer the file
          maxFilesize: 2, // MB
      
          // Whether to send multiple files in one request. 
          // If this it set to true, then the fallback file input element will 
          // have the multiple attribute as well.
          uploadMultiple: TRUE,
      
          params: {
              'gallery_id' : 1 // Your gallery Id
          }
      };
      

      这与在表单元素中添加隐藏输入字段相同。

      【讨论】:

      • 好的,我会试一试,但我想我必须更改我的代码。因为在按钮中使用相同的 id 是错误的。我必须先弄清楚,因为按钮在 foreach 中,所以它具有相同的 id。
      【解决方案3】:

      您可以使用在发送每个文件之前调用的 dropzone 事件方法,此方法有 3 个参数 file:对于将要发送的文件,xhr 用于数据传输,formData 将包含您的数据

      在你的情况下你必须这样做:

      Dropzone.on('sending', function(file, xhr, formData){
      formData.append('gallery_id',  put_here_your_id )
      
      }
      

      在 laravel 控制器中你可以读取你的 id :

      $request->galery_id
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2020-04-21
        • 1970-01-01
        • 1970-01-01
        • 2020-03-28
        • 2019-08-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多