简体   繁体   English

Dropzone.js始终自动上传文件。 无法在Dropzone #id上设置选项-Laravel-Voyager-Dropzone.js

[英]Dropzone.js always auto uploads files. Can't set options on dropzone #id - Laravel - Voyager - Dropzone.js

i want to create a dropzone in my web application for uploading images and manipulate them with ImageMagick. 我想在我的Web应用程序中创建一个dropzone来上传图像并使用ImageMagick对其进行操作。 My dropzone always auto uploads all Images and show an "opject Object" Error on the previews of the images in the dropzone. 我的拖放区始终自动上传所有图像,并在拖放区中的图像预览上显示“对象对象”错误。 The upload on the server works, but i want to add Dropzone.options.myAwesomeDropzone to my dropzone to upload the images when i submit a button because i also want to send data from a form while uploading. 在服务器上进行上传是可以的,但是我想在提交按钮时将Dropzone.options.myAwesomeDropzone添加到我的dropzone来上传图像,因为我也想在上传时从表单发送数据。

Heres how i implemented it in the view: 这是我在视图中如何实现的方法:

$    <div class="dropzone" id="my-awesome-dropzone">

The .js in the view (otherwise it doesn't work) 视图中的.js(否则它将不起作用)

<script src="https://cdnjs.cloudflare.com/ajax/libs/dropzone/5.4.0/dropzone.js"></script>

<script type="text/javascript">
    var baseUrl = "{{ url('/') }}";
    var token = "{{ csrf_token() }}";
    var myDropzone = new Dropzone("div#my-awesome-dropzone", {
        url: baseUrl + "/upload",
        params: {
            _token: token
        }
    });

My Controller: 我的控制器:

public function upload()
    {
        $input = Input::all();

        $rules = array(
            'file' => 'image|max:3000',
        );

        $validation = Validator::make($input, $rules);

        if ($validation->fails()) {
            return Response::make($validation->errors->first(), 400);
        }

        $destinationPath = 'uploads'; // upload path
        $extension = Input::file('file')->getClientOriginalExtension(); // getting file extension
        $fileName = rand(11111, 99999) . '.' . $extension; // renameing image
        $upload_success = Input::file('file')->move($destinationPath, $fileName); // uploading file to given path

        if ($upload_success) {
            return Response::json('success', 200);
        } else {
            return Response::json('error', 400);
        }


}

I hope someone can help, i was searching though the internet for hours but couldn't find something that fixes my problem. 我希望有人能提供帮助,我在互联网上搜索了好几个小时,但找不到能解决我的问题的东西。 There are hundreds of solution which work for everyone but me.... 有数百种解决方案,对我以外的所有人都适用。

Best regards 最好的祝福

If you take a look at the docs of dropzone, it says that in configuration you have to set the prop autoProcessQueue to false and then call myDropzone.processQueue() 如果查看一下dropzone的文档,它说在配置中必须将prop autoProcessQueue设置为false,然后调用myDropzone.processQueue()

So try something with the looks of this: 因此,尝试使用以下外观:

var myDropzone;
var token = "{{ csrf_token() }}";
var baseUrl = "{{ url('/') }}";
$(document).ready(function(){
      myDropzone = new Dropzone("div#my-awesome-dropzone", {
      url: baseUrl + "/upload",
      params: {
         _token: token
         // other fields, here you can also pass a function and have the function return the fields
         name: $("#name").val()
      },
      autoProcessQueue:false,
   });
})

$("#yourButtonId",function(e){
   e.preventDefault();
   myDropzone.processQueue();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM