【发布时间】:2016-08-06 06:55:53
【问题描述】:
使用dropzone.js 我正在为我的滑块上传图片。当我将文件上传到服务器时,响应只是一个与图像/文件数据库 ID 对应的数字。如果有文件列表,则有一个 id 列表作为响应。现在我想出于某种目的将此 id 分配给 dropzone 文件。
【问题讨论】:
使用dropzone.js 我正在为我的滑块上传图片。当我将文件上传到服务器时,响应只是一个与图像/文件数据库 ID 对应的数字。如果有文件列表,则有一个 id 列表作为响应。现在我想出于某种目的将此 id 分配给 dropzone 文件。
【问题讨论】:
上传文件时会触发一个成功事件,附加到该事件,下面的示例代码(this 是 dropzone 参考)
this.on("success",
function(file, responseStr) {
console.log(responseStr);
var responseObj = JSON.parse(responseStr);
if (responseObj.success) {
// save response for later processing
file.additionalInfo = responseObj;
} else {
var message = responseObj.message;
file.previewElement.classList.add("dz-error");
file.status = Dropzone.ERROR;
var els = file.previewElement.querySelectorAll("[data-dz-errormessage]");
for (var i = 0; i < els.length; i++) {
els[i].textContent = message;
}
}
});
【讨论】:
对于多个文件,您可以使用 dropzone 提供的“multilefiles”事件,或者只是将每个文件的响应迭代为 下面:
this.on("success", function (file, response)
{
// if multiple files are saved the response returns all the documents
// but dropzone will fire the success event per file
// find the corresponding document based on name
if (response != null && response.Documents != null)
{
var d = response.Documents.find(x => x.FileName == file.name);
refresh = true;
dropZone.emit("thumbnail", file, d.ThumbnailBase64);
}
});
【讨论】:
成功设置预览“id”属性
myDropzone.on("success", function(file,data) {
let myfile = file.previewTemplate;
myfile.setAttribute('id',data.server_data.id);
});
在删除操作时获取此 ID
myDropzone.on("removedfile", function(file) {
var server_file = $(file.previewTemplate);
var id = server_file.attr("id");
$.ajax({
url: your url,
type: "DELETE",
data: { "_token":_token,'id':id },
success: (data) => {
console.log(data);
},
error: function(data){
console.log(data);
}
});
});
【讨论】: