如何通过额外的请求动态更改Dropzone.js中的URL?

时间:2017-01-19 03:55:44

标签: javascript asynchronous dropzone.js synchronous

我需要每个上传的文件都有不同的网址。如果事先知道这些上传网址,可以通过以下方式轻松完成:

Dropzone.options.myDropzone = {
  init: function() {
    this.on("processing", function(file) {
      this.options.url = "/some-other-url";
    });
  }
};

但是,在我的情况下,必须在提交文件之前从服务器请求每个URL。我想要的是只有在ajax请求(对于上传URL)的结果返回时才开始处理的方法。有没有办法让处理等待请求的回调?

如果我发出同步请求怎么办,如下所示?鉴于要求很小,是否存在重大缺点?

Dropzone.options.myDropzone = {
  init: function() {
    this.on("processing", function(file) {
      // synchronous request
      var xhr = new XMLHttpRequest();
      xhr.open("GET", "give-me-upload-url-please", false);
      // synchronous request with false
      xhr.send(null);
      this.options.url = xhr.responseText;
    });
  }
};

谢谢。

0 个答案:

没有答案