[英]how to upload and delete files from dropzone.js
I have used the below code the image has been deleted but the thumbnail image still showing.我使用了下面的代码,图像已被删除,但缩略图仍然显示。
Dropzone.options.myDropzone = {
init: function() {
this.on("success", function(file, response) {
file.serverId = response;
});
this.on("removedfile", function(file) {
if (!file.serverId) { return; }
$.post("delete-file.php?id=" + file.serverId);
});
}
For deleting thumbnails you have to enable addRemoveLinks: true, and to use "removedfile" option in dropzonejs要删除缩略图,您必须启用 addRemoveLinks: true,并在 dropzonejs 中使用“removedfile”选项
removedfile: Called whenever a file is removed from the list. removefile:每当从列表中删除文件时调用。 You can listen to this and delete the file from your server if you want to.如果您愿意,您可以收听此内容并从您的服务器中删除该文件。
addRemoveLinks: true,
removedfile: function(file) {
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
}
I also added an ajax call for delete script and it looks like this:我还添加了一个删除脚本的ajax调用,它看起来像这样:
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
$.ajax({
type: 'POST',
url: 'delete.php',
data: "id="+name,
dataType: 'html'
});
var _ref;
return (_ref = file.previewElement) != null ? _ref.parentNode.removeChild(file.previewElement) : void 0;
}
It works on my side, so I hope it helps.它对我有用,所以我希望它有所帮助。
In addition to the best answer above - to remove spaces and replace with dashes and convert to lower case apply this js in the dropzone.js file:除了上面的最佳答案 - 要删除空格并替换为破折号并转换为小写,请在 dropzone.js 文件中应用此 js:
name = name.replace(/\s+/g, '-').toLowerCase();
To the filename handling - I edited the dropzone.js
file AND the above ajax call.对于文件名处理 - 我编辑了dropzone.js
文件和上面的 ajax 调用。 This way, the client handles the file naming, and it AUTOMATICALLY gets sent to the PHP/server-side, so u don't have to redo it there, and it's URL safe pretty much.这样,客户端处理文件命名,并自动发送到 PHP/服务器端,所以你不必在那里重做,而且它的 URL 几乎是安全的。
In some instances the js changed depending on the function / naming:在某些情况下,js 会根据函数/命名发生变化:
dropzone.js - line 293 (approx): dropzone.js - 第 293 行(大约):
node = _ref[_i];
node.textContent = this._renameFilename(file.name.replace(/\s+/g, '-').toLowerCase());
dropzone.js - line 746 (approx): dropzone.js - 第 746 行(大约):
Dropzone.prototype._renameFilename = function(name) {
if (typeof this.options.renameFilename !== "function") {
return name.replace(/\s+/g, '-').toLowerCase();
}
return this.options.renameFilename(name.replace(/\s+/g, '-').toLowerCase());
};
I moved the whole removedFile
section up to the top of dropzone.js
like so:我将整个removedFile
部分移动到dropzone.js
的顶部,如下所示:
autoQueue: true,
addRemoveLinks: true,
removedfile: function(file) {
var name = file.name;
name =name.replace(/\s+/g, '-').toLowerCase(); /*only spaces*/
$.ajax({
type: 'POST',
url: 'dropzone.php',
data: "id=" + name,
dataType: 'html',
success: function(data) {
$("#msg").html(data);
}
});
var _ref;
if (file.previewElement) {
if ((_ref = file.previewElement) != null) {
_ref.parentNode.removeChild(file.previewElement);
}
}
return this._updateMaxFilesReachedClass();
},
previewsContainer: null,
hiddenInputContainer: "body",
Note I also added in a message box in the html: ( div id="msg"></div>
) in the HTML that will allow server side error handling/deleting to post a message back to the user as well.注意我还在 html 中添加了一个消息框:( div id="msg"></div>
)在 HTML 中,这将允许服务器端错误处理/删除也将消息发回给用户。
in dropzone.php:在 dropzone.php 中:
if (isset($_POST['id']) {
//delete/unlink file
echo $_POST['id'] . ' deleted'; // send msg back to user
}
This is only an expansion with the working code on my side.这只是我这边工作代码的扩展。 I have 3 files, dropzone.html, dropzone.php, dropzone.js我有 3 个文件,dropzone.html、dropzone.php、dropzone.js
Obviously, you would create a js function instead of repeating the code, but it suited me since the naming changes.显然,您将创建一个 js 函数而不是重复代码,但由于命名更改,它适合我。 You could pass the variables in a js function yourself to handle filename spaces/chars / etc.您可以自己在 js 函数中传递变量来处理文件名空间/字符/等。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.