簡體   English   中英

Dropzone未定義

[英]Dropzone is not defined

我是JavaScript的新手,這讓我發瘋。

我想使用Dropzone.js,所以我從這里下載了文件dropzone.js並將其包含在我的視圖中,如下所示:

<script src="<?php echo JS_DIRECTORY; ?>/dropzone.js"></script>

然后我創建了這樣的表單:

<form action="http://localhost/project/uploadTest/upload" class="dropzone">
</form>

它工作正常。 它指向php功能,我處理服務器站點上傳。

問題是當我想訪問JS中的dropzone對象來配置它時。 當我做

// "myAwesomeDropzone" is the camelized version of the HTML element's ID
Dropzone.options.myAwesomeDropzone = {
  paramName: "file", // The name that will be used to transfer the file
  maxFilesize: 2, // MB
  accept: function(file, done) {
    if (file.name == "justinbieber.jpg") {
      done("Naha, you don't.");
    }
    else { done(); }
  }
};

我明白了

未捕獲的ReferenceError:未定義Dropzone

任何幫助將不勝感激,謝謝

您的代碼可能運行得太快。 包裹它:

window.onload = function() {
    // access Dropzone here
};

或者,更好(比上面的代碼早運行):

document.addEventListener("DOMContentLoaded", function() {
    // access Dropzone here
});

或者,如果你使用jQuery

$(function() {
    // access Dropzone here
});

按照這個:

你的HTML文件:

<form action="your url" class="dropzone" id="dropzone-form">
</form>

你的JS檔案:

window.onload = function() {
    // dropzoneFormis the configuration for the element that has an id attribute
    // with the value dropzone-form (or dropzoneForm)
    //initialize the dropzone;
    Dropzone.options.dropzoneForm = {
            autoProcessQueue: 'your value',
            acceptedFiles: 'your value',
            maxFilesize: 'your value',
            ....and so on.
            init: function() {
               myDropzone = this;

               this.on('addedfile', function(file) {
                   //todo...something...
               }
               //catch other events here...
            }
    };
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM