2013-07-20 118 views
46
已经存储在服务器上的文件

我不明白...... call它总是不确定我怎么能告诉你使用Dropzone.js

创建模拟文件:

var mockFile = { name: "Filename", size: 12345 }; 

调用默认addedfile事件处理

myDropzone.options.addedfile.call(myDropzone, mockFile); 

和可选显示文件的缩略图:

myDropzone.options. thumbnail.call(myDropzone, mockFile, "/image/url"); 

回答

37

终于!!

$(function() { 
    var mockFile = { name: "banner2.jpg", size: 12345 }; 
    var myDropzone = new Dropzone("#my-awesome-dropzone"); 
    myDropzone.options.addedfile.call(myDropzone, mockFile); 
    myDropzone.options.thumbnail.call(myDropzone, mockFile, "http://localhost/test/drop/uploads/banner2.jpg"); 
}) 
+3

值得注意的是,使用此方法添加的文件与“常规”文件的行为有些不同。首先,在dropzone实例上调用.removeAllFiles()时,我似乎无法摆脱它们。我发布这个作为一个问题,以防万一有人想贡献:http://stackoverflow.com/questions/23369291/dropzone-js-removeallfiles-does-not-remove-mock-files –

+7

这是因为你实际上没有添加任何文件到文件数组。使用thisDropzone.files.push(mockFile); – Dan

+0

@dan在哪里添加推送功能,我在thumbnail.call下面添加,但它不工作。 –

0

我当时也很难过。这一个作为出发点会帮助更多:

Dropzone.autoDiscover = false; // otherwise will be initialized twice 
var myDropzoneOptions = { 
    maxFilesize: 5, 
    addRemoveLinks: true, 
    clickable: true 
}; 
var myDropzone = new Dropzone('#myDropzoneElement', myDropzoneOptions); 
var mockFile = { name: "Existing file!", size: 12345 }; 
myDropzone.options.addedfile.call(myDropzone, mockFile); 
myDropzone.options.thumbnail.call(myDropzone, mockFile, "http://url-to-thumb-goes-here"); 
30

您也可以用下面的代码:

<script> 
     Dropzone.options.myAwesomeDropzone = false; 
     Dropzone.autoDiscover = false; 
     $("#image").dropzone({ 
      url: "http://someserver.com/upload.php", 
      paramName: "image", // The name that will be used to transfer the file 
      maxFilesize: 2, // MB 
      maxFiles: 5, 
      parallelUploads: 5, 
      addRemoveLinks: true, 
      dictMaxFilesExceeded: "You can only upload upto 5 images", 
      dictRemoveFile: "Delete", 
      dictCancelUploadConfirmation: "Are you sure to cancel upload?", 
      accept: function (file, done) { 
       console.log(file) 
       if ((file.type).toLowerCase() != "image/jpg" && 
         (file.type).toLowerCase() != "image/gif" && 
         (file.type).toLowerCase() != "image/jpeg" && 
         (file.type).toLowerCase() != "image/png" 
         ) { 
        done("Invalid file"); 
       } 
       else { 
        done(); 
       } 
      }, 
      init: function() { 
       var mockFile = { name: "myimage.jpg", size: 12345, type: 'image/jpeg' }; 
       this.addFile.call(this, mockFile); 
       this.options.thumbnail.call(this, mockFile, "http://someserver.com/myimage.jpg"); 
      } 
     }); 
    </script> 

编辑

由于悬浮窗4.0 init功能的更新可以被称为:

init: function() { 
    var mockFile = { name: "myimage.jpg", size: 12345, type: 'image/jpeg' };  
    this.options.addedfile.call(this, mockFile); 
    this.options.thumbnail.call(this, mockFile, "http://someserver.com/myimage.jpg"); 
    mockFile.previewElement.classList.add('dz-success'); 
    mockFile.previewElement.classList.add('dz-complete'); 
} 
+2

fwiw,我发现'addFile'错误在最新的dropzone的铬,使用'选项。 addedfile.call'工作,而不是 –

+0

我如何取消手动添加文件(S)的removeLink?请帮助 – Adobe

+0

@Adobe你可以更具体吗? –

-2

您可以使用此

var file_image = "http://someserver.com/myimage.jpg"; 

var mockFile = { name: "myimage.jpg", size: 12345 }; 

$("#dropzone").dropzone({ 

    url: 'false', 
    maxFiles: 1, 
    maxFilesize:10,//mb 
    acceptedFiles:'image/*', 
    init: function() { 
     this.on("addedfile", function(file){ 
      this.options.thumbnail.call(this,file,file_image); 
     }); 
     this.addFile.call(this,mockFile); 
    } 
}); 
16

我为> = 4.0解决方案尝试,立足于 “如何显示已存储在服务器上的文件”:https://github.com/enyo/dropzone/wiki/FAQ

maxFiles: 1, 

init: function() { 
    this.on('maxfilesexceeded', function (file) { 
     this.removeAllFiles(); 
     this.addFile(file); 
    }); 

    var mocks = $dropzone.data('dropzone'); 
    for (var i = 0; i < mocks.length; i++) { 
     var mock = mocks[i]; 
     mock.accepted = true; 

     this.files.push(mock); 
     this.emit('addedfile', mock); 
     this.createThumbnailFromUrl(mock, mock.url); 
     this.emit('complete', mock); 
    } 
} 
+0

这是在我看来是正确的答案,它正确处理使用'resize'和'maxFiles'选项 –

+0

如何指定缩略图样式?我创建了一个B&W版本,我想以缩略图的形式显示,该怎么做? –

+0

我的问题是我的文件的缩略图部分显示,我使用“this.emit(”thumbnail“,mockFile,item.Path);” 。我将其替换为“this.createThumbnailFromUrl(mock,mock.url);”它的工作。 –

4

在此应答https://stackoverflow.com/a/17763511,它implemeneted与emmiting一个缩略图事件。

以下是使用createThumbnailFromUrl的示例。

HTML元素;

<form id="sample-img" action="/upload" class="dropzone"> 
    <div class="dz-default dz-message"></div> 
</form> 

JS代码;

previewThumbailFromUrl({ 
    selector: 'sample-img', 
    fileName: 'sampleImg', 
    imageURL: '/images/sample.png' 
}); 

function previewThumbailFromUrl(opts) { 
    var imgDropzone = Dropzone.forElement("#" + opts.selector); 
    var mockFile = { 
     name: opts.fileName, 
     size: 12345, 
     accepted: true, 
     kind: 'image' 
    }; 
    imgDropzone.emit("addedfile", mockFile); 
    imgDropzone.files.push(mockFile); 
    imgDropzone.createThumbnailFromUrl(mockFile, opts.imageURL, function() { 
     imgDropzone.emit("complete", mockFile); 
    }); 
} 
上的jsfiddle

工作样本:

  1. Load images on same domain
  2. Load images with crossOrigin
+0

Uncaught SecurityError:未能在'CanvasRenderingContext2D'上执行'getImageData':画布已被交叉源数据污染。 – Justin

+0

@Justin,这里是我的上述代码https://jsfiddle.net/gihanchanuka/pp66q18p/1的工作示例。由于您通过HTTPS获得了跨域问题,并不表示上述代码不适用于相同的域图像URL。正如你可以清楚地看到我的答案'imageURL:'/ images/sample.png',它指的是在同一个域中的图像。这是建立在我的知识基础上的解决方案。您也可以寻求其他解决方案。这里是更多关于您的错误http://stackoverflow.com/a/27840082/1461060。希望对你有帮助! – gihanchanuka

+0

@Justin我看了一下DropzoneJS的实现https://github.com/antpaw/dropzone/blob/2a960cf89c70b0eb8b73d65fbbcc56cce9ae516f/src/dropzone.coffee#L1009,并找出了你无法解决的问题。 DropzoneJS允许传递'crossOrigin'的配置。请参阅https://developer.mozilla.org/en-US/docs/Web/HTML/Element/img。我已用'crossOrigin'选项更新了答案。 https://jsfiddle.net/gihanchanuka/6gksmdrr/1/ – gihanchanuka

4

基于以上朋克的出色答卷,你不应该忘记在末尾添加this._updateMaxFilesReachedClass();,像所以:

init: function() { 
    var mockFile = { name: <filename>, size: <filesize>, type: <filetype>, url: <file_url> }; 
    this.files.push(mockFile); 
    this.emit('addedfile', mockFile); 
    this.createThumbnailFromUrl(mockFile, mockFile.url); 
    this.emit('complete', mockFile); 
    this._updateMaxFilesReachedClass(); 
} 
+1

为了使它工作,有必要将文件设置为接受,否则不会发出事件** maxfilesreached **(请参阅_updateMaxFilesReachedClass代码)。 快速解决方案是将** accepted:true **添加到mockFile。你怎么看? –

+1

@MauroNidola你是对的。使用'this._updateMaxFilesReachedClass();'没有意义;'如果你不添加'accepted:true'到mockFile。 –

相关问题