动态更改Dropzone网址

时间:2018-08-28 17:43:45

标签: javascript dropzone.js

我正在尝试在dropzone中更改URL,但是我发现的信息似乎不适用于我。我有一种使用JS放置dropzone的形式。我想根据下拉菜单中选择的选项来更改dropzone URL。这是我正在使用的代码:

Dropzone

<select id="bioImages" name="bioImages" style="width: 300px;" onchange="urlSetter();">
        <option value="countryFlag">Country Flag</option>
        <option value="locationImg">Location Image</option>
        <option value="nationalAnthemaud">National Anthem</option>
        <option value="countryNameaud">Country Name</option>
</select>
<form id="dropzoneUpload" action="" enctype="multipart/form-data" method="post">
    <div class="clearfix">
        <br><br>
    </div>
    <div class="half clearfix">
        <div id="dZUpload" class="dropzone" name="fileUpload">
        <div class="dz-default dz-message">
            Drop image/audio here or click to upload (system supports 1 file per category).
        </div>
        </div>
     </div>
</form>

添加Dropzone

function addDropzone() {
    $(document).ready(function () {
        Dropzone.autoDiscover = false;
        $("#dZUpload").dropzone({
            url: "url.com",
            addRemoveLinks: true,
            success: function (file, response) {
                console.log(response);
             }
        });
    });
}

更改URL

function urlSetter() {
    var drop = document.getElementById('dropzoneUpload');
    if(type.includes('aud')) {
        drop.options.url = 'url.com';
    } else {
        drop.options.url = 'url2.org';
    }
}

2 个答案:

答案 0 :(得分:1)

您不能使用这种方式更改dropzone网址。为此,首先创建一个全局变量,然后在下拉更改事件(urlSetter)上设置该变量的值,如下所示:

var dropzonePostUrl='url.com';
function urlSetter() {
    var drop = document.getElementById('dropzoneUpload');
    if(type.includes('aud')) {
        dropzonePostUrl = 'url.com';
    } else {
        dropzonePostUrl = 'url2.org';
    }
}

您还需要按如下所示更改您的dropzone初始化代码:

function addDropzone() {
    $(document).ready(function () {
        Dropzone.autoDiscover = false;
        $("#dZUpload").dropzone({
            url: "url.com",
            addRemoveLinks: true,
            success: function (file, response) {
                console.log(response);
             },
            init: function () {
               var _this=this;
                this.on("processing", function (file) {
                    _this.options.url = dropzonePostUrl;
                });
            }
        });
    });
}

您可以在处理事件中更改网址。

答案 1 :(得分:0)

function addDropzone() {
  $(document).ready(function () {
    Dropzone.autoDiscover = false;
    $("#dZUpload").dropzone({
      url: "url.com",
      addRemoveLinks: true,
      success: function (file, response) {
      	console.log(response);
      },
      init: function () {
      	this.on("processing", function (file) {
        	this.options.url = dropzonePostUrl;
       	});
      }
    });
  });
}
var dropzonePostUrl='url.com';
function urlSetter() {
  var drop = document.getElementById('dropzoneUpload');
  if(type.includes('aud')) {
		dropzonePostUrl = 'url.com';
  } else {
		dropzonePostUrl = 'url2.org';
  }
}
<select id="bioImages" name="bioImages" style="width: 300px;" onchange="urlSetter();">
  <option value="countryFlag">Country Flag</option>
  <option value="locationImg">Location Image</option>
  <option value="nationalAnthemaud">National Anthem</option>
  <option value="countryNameaud">Country Name</option>
</select>
<form id="dropzoneUpload" action="" enctype="multipart/form-data" method="post">
	<div class="clearfix">
	  <br><br>
	</div>
	<div class="half clearfix">
	  <div id="dZUpload" class="dropzone" name="fileUpload">
		  <div class="dz-default dz-message">
		  	Drop image/audio here or click to upload (system supports 1 file per category).
		  </div>
	  </div>
	</div>
</form>