Dropzone JS 等到 ajax 表单提交并更改 options.url

Dropzone JS Wait until ajax form submitted and change options.url

本文关键字:options url 表单提交 JS 等到 ajax Dropzone      更新时间:2023-09-26

我有带有dropzonejs插件的Rails应用程序来上传图像。我在上传图像之前创建Album。然后我想将图片上传到新相册。

Dropzone.options.myDropzone = {
  init: function() {
    this.on("processing", function(file) {
    # Performing album creation via $.ajax
    # $.ajax return ID of an album, but JS already skipped before I set new URL
    });
  }
};

如何等到请求完成并正确将返回的 ID 设置为 url 选项?

在这种情况下,您可以使用 accept -

Dropzone.options.myDropzone = {
  accept: function(file, done)
        {
            file.postData = {};
            $.ajax({
                url: '/album',
                data: {name: file.name, type: file.type},
                type: 'POST',
                success: function(response)
                {
                    file.album_id = response.album_id;
                    done();
                },
                error: function(response)
                {
                    done('error preparing the file');
                }
            });
        },
  init: function() {
    this.on("processing", function(file) {
      # Do other stuffs if needed
    });
  }
};

现在,dropbox将等到drop()被调用。