如何使用 DropZone 插件编辑和上传现有图像 Laravel JS
How to Edit and upload existing images with DropZone plugin Laravel JS
我尝试将 dropzone 添加到我的项目中以对图像进行处理。我需要知道如何添加或删除服务器中的现有图像并更新文件。我已经完成了创建部分,但对于编辑部分我卡住了。这是我下面的代码,用于获取现有图像和上传图像。
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();
}
});
}
});
我需要编辑现有图像并更新新图像。
此代码是为 Laravel blade 文件编写的:
@foreach ($product->attachments as $attach)
<?php $path = Helper::image_to_base64(asset($attach->url)); ?>
<script>
$("document").ready(()=>{
var path = "{{ $path }}";
var file = new File([path], "{{ $attach->file_name }}", {type: "{{ $attach->mime_type }}", lastModified: {{ $attach->updated_at}}})
file['status'] = "queued";
file['status'] = "queued";
file['previewElement'] = "div.dz-preview.dz-image-preview";
file['previewTemplate'] = "div.dz-preview.dz-image-preview";
file['_removeLink'] = "a.dz-remove";
file['webkitRelativePath'] = "";
file['width'] = 500;
file['height'] = 500;
file['accepted'] = true;
file['dataURL'] = path;
file['upload'] = {
bytesSent: 0 ,
filename: "{{ $attach->file_name }}" ,
progress: 0 ,
total: {{ $attach->file_size }} ,
uuid: "{{ md5($attach->id) }}" ,
};
myDropzone.emit("addedfile", file , path);
myDropzone.emit("thumbnail", file , path);
// myDropzone.emit("complete", itemInfo);
// myDropzone.options.maxFiles = myDropzone.options.maxFiles - 1;
myDropzone.files.push(file);
console.log(file);
});
</script>
@endforeach
我尝试将 dropzone 添加到我的项目中以对图像进行处理。我需要知道如何添加或删除服务器中的现有图像并更新文件。我已经完成了创建部分,但对于编辑部分我卡住了。这是我下面的代码,用于获取现有图像和上传图像。
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();
}
});
}
});
我需要编辑现有图像并更新新图像。
此代码是为 Laravel blade 文件编写的:
@foreach ($product->attachments as $attach)
<?php $path = Helper::image_to_base64(asset($attach->url)); ?>
<script>
$("document").ready(()=>{
var path = "{{ $path }}";
var file = new File([path], "{{ $attach->file_name }}", {type: "{{ $attach->mime_type }}", lastModified: {{ $attach->updated_at}}})
file['status'] = "queued";
file['status'] = "queued";
file['previewElement'] = "div.dz-preview.dz-image-preview";
file['previewTemplate'] = "div.dz-preview.dz-image-preview";
file['_removeLink'] = "a.dz-remove";
file['webkitRelativePath'] = "";
file['width'] = 500;
file['height'] = 500;
file['accepted'] = true;
file['dataURL'] = path;
file['upload'] = {
bytesSent: 0 ,
filename: "{{ $attach->file_name }}" ,
progress: 0 ,
total: {{ $attach->file_size }} ,
uuid: "{{ md5($attach->id) }}" ,
};
myDropzone.emit("addedfile", file , path);
myDropzone.emit("thumbnail", file , path);
// myDropzone.emit("complete", itemInfo);
// myDropzone.options.maxFiles = myDropzone.options.maxFiles - 1;
myDropzone.files.push(file);
console.log(file);
});
</script>
@endforeach