在dropzone.js中启用复制和粘贴文件
Enable copy and paste files in dropzone.js
我正在使用dropzone.js
。我想在其中实现"复制和粘贴">功能。
我尝试的是:
Inside dropzone.js:
paste: function(e) {
Dropzone.prototype.emit("paste");
}
Dropzone.prototype.paste = function(e) {
var items, _ref;
if ((e != null ? (_ref = e.clipboardData) != null ? _ref.items : void 0 : void 0) == null) {
return;
}
this.emit("paste", e);
items = e.clipboardData.items;
if (items.length) {
return this._addFilesFromItems(items);
}
};
Page level script:
<script>
var dropZone = Dropzone.forElement('#dropzone1');
dropZone.paste();
</script>
以上不是调用paste:function(e){..}
如何纠正?
如果你不想使用其他JS库,你可以通过从粘贴事件中以文件的形式检索数据,很容易地将dropzone与粘贴事件集成:
// create dropzone however you wish
const myDropzone = new Dropzone("div#element", { url: "/path/to/upload"});
// add paste event listener to the page
document.onpaste = function(event){
const items = (event.clipboardData || event.originalEvent.clipboardData).items;
items.forEach((item) => {
if (item.kind === 'file') {
// adds the file to your dropzone instance
myDropzone.addFile(item.getAsFile())
}
})
}
这对我很有用。它使用了FileReaderJS包装器。由于我不是用程序创建dropzone的,所以我不得不在运行时使用init((方法存储它。
请参阅此处获取FileReaderJS部分。
var myDropzone;
function checkUploadFile(filename) {
//do some input checking here, if you want
return true;
}
Dropzone.options.FileDropUploadZone = {
paramName: "myDiv",
maxFilesize: 3, // MB
uploadMultiple: true,
addRemoveLinks: true,
acceptedFiles: 'image/*',
maxFiles: 10,
accept: function(file, done) {
if (!checkUploadFile(file.name)) {
done('Invalid file');
myDropzone.removeFile(file);
}
else { done(); }
},
init: function() {
myDropzone = this;
}
};
$(document).ready(function () {
FileReaderJS.setupClipboard(document.body, {
accept: {
'image/*': 'DataURL'
},
on: {
load: function(e, file) {
myDropzone.addFile(file);
}
}
});
});
var myDropzone = new Dropzone(".dropzone", { });
document.onpaste = function(event){
var items = (event.clipboardData || event.originalEvent.clipboardData).items;
for (index in items) {
var item = items[index];
if (item.kind === 'file') {
// adds the file to your dropzone instance
myDropzone.addFile(item.getAsFile())
}
}
}
只需添加此代码。不要声明URL
,因为URL
也在PHP或编码文件中声明,请将此代码粘贴到视图文件(HTML、PHP等(中。
相关文章:
- 如何在android中使用phonegap将文件从一个文件夹移动/复制到另一个文件夹
- 将文件从应用程序文件夹复制到JQM/Phonegap应用程序中的根文件夹
- jqueryajax调用是复制url,当在模态视图之后在外部js文件中进行调用时
- 使用SeleniumWebdriver将文本复制到文件时出现编译错误的解决方案
- 构建一些文件,复制其余文件
- 日食添加文件而不将其复制到工作区中
- 如何设置观察程序将文件复制到同一文件夹中的其他文件
- 如何在刷新时停止我的 xml 文件复制但续订
- 无法获取文件复制进度
- 将内容从一个文件复制到另一个文件
- 使用网页将本地文件复制到另一个位置
- 将文件复制到子目录
- 如何使用webpack将文件复制到分发文件夹
- Grunt,在构建时将html文件复制到脚本文件夹中
- 将数据从记事本/Excel文件复制到Excel文件“;按列”;
- gullowwatch将文件复制两次到不同的位置
- Gulp sass将空的scss文件复制到目标文件夹
- 如何从javascript文件复制一个href
- Javascript include_once- Javascript PHP调用相同的文件复制内容
- JavaScript'输入倍数'文件复制