【发布时间】:2021-05-23 18:26:33
【问题描述】:
我尝试将 dropzone 添加到我的项目中以在图像中进行 crud。我需要知道如何在服务器中添加或删除现有图像并更新文件。我已经完成了创建部分,但对于编辑部分我卡住了。这是我下面的代码,用于获取现有图像并上传图像。
Dropzone.autoDiscover = false;
$("#dZUploadEdit").dropzone({ // initialize
createImageThumbnails: true,
autoProcessQueue: false,
uploadMultiple: true,
parallelUploads: 5,
addRemoveLinks: true,
url: "product/" + id, // upload route
type: "PUT",
maxFiles: 5,
data: {
_token: $('meta[name="csrf-token"]').attr("content")
},
init: function() { // start of getting existing imahes
myDropzone = this;
$.ajax({
url: "product/images",
type: "POST",
data: {
id: id,
_token: $('meta[name="csrf-token"]').attr("content")
},
dataType: "json",
success: function(response) { // get result
$.each(response, function(key, value) {
var mockFile = {
name: value.name,
size: value.size
};
myDropzone.options.addedfile.call(
myDropzone,
mockFile
);
myDropzone.options.thumbnail.call(
myDropzone,
mockFile,
"storage/products/" + value.name
);
$("[data-dz-thumbnail]").css("height", "120");
$("[data-dz-thumbnail]").css("width", "120");
$("[data-dz-thumbnail]").css("object-fit", "cover");
});
// Update selector to match your button
$("#update-btn").on("click", function(e) {
e.preventDefault();
myDropzone.processQueue();
});
myDropzone.on("sending", function(file, xhr, formData) {
// Append all form inputs to the formData Dropzone will POST
var data = $("#editForm").serializeArray();
$.each(data, function(key, el) {
formData.append(el.name, el.value);
});
});
}
});
},
success: function(file, response) {
file.previewElement.classList.add("dz-success");
return location.reload();
},
error: function(file, errorMessage) {
errors = true;
Swal.fire({
icon: "error",
title: "Oops...",
text: "Something went wrong!",
footer: "Changes are not saved!"
}).then(result => {
if (result.value) {
return location.reload();
}
});
}
});
我需要编辑现有图像并更新新图像。
【问题讨论】:
标签: jquery laravel dropzone.js