Implementada funcionalidad descarga en zip

This commit is contained in:
Ignacio Martinez Navajas
2025-05-22 22:10:14 +02:00
parent b084d1bd7c
commit e3c202ae64
8 changed files with 188 additions and 19 deletions

View File

@ -30,6 +30,7 @@ class FileUploadDropzone {
this.modelId = this.jqElement.data('id')
this.btnSelectFiles = $(`#${domElement.replace('#', '')}_btnUploadFiles`)
this.btnSubmitFile = $(`#${domElement.replace('#', '')}_btnSubmitFiles`)
this.btnDownloadFiles = $(`#${domElement.replace('#', '')}_btnDownloadFiles`);
this.dataPost = {}
this.nameId = nameId;
this.getUri = getUri
@ -44,6 +45,7 @@ class FileUploadDropzone {
this.btnSelectFiles.on('click', () => {
this.jqElement.trigger('click')
})
this.btnDownloadFiles.on('click', this._handleDownloadFiles.bind(this))
this.dropzone = new Dropzone(this.domElement, {
url: this.postUri,
@ -67,12 +69,12 @@ class FileUploadDropzone {
var viewButton = Dropzone.createElement("<span class='dz-remove'>Ver</span>");
file.previewElement.appendChild(viewButton);
// Listen to the view button click event
viewButton.addEventListener("click", this.onViewButton.bind(this,file));
viewButton.addEventListener("click", this.onViewButton.bind(this, file));
}
}
onViewButton(file) {
console.log(window.location.protocol + "//" + window.location.host + "/sistema/intranet/" + this.resourcePath + "/" + file.hash)
window.open(window.location.protocol + "//" + window.location.host + "/sistema/intranet/" + this.resourcePath + "/" + file.hash, '_blank');
window.open(window.location.protocol + "//" + window.location.host + "/sistema/intranet/" + this.resourcePath + "/" + file.hash, '_blank');
}
_getDropzoneFilesFormData() {
var files = this.dropzone.files;
@ -134,6 +136,47 @@ class FileUploadDropzone {
}
}
_handleDownloadFiles() {
$("#loader").modal('show');
$.ajax({
url: `/presupuestoadmin/download_zip`,
type: 'POST',
data: {
[this.nameId]: this.modelId
},
xhrFields: {
responseType: 'blob'
},
success: (blob, status, xhr) => {
const disposition = xhr.getResponseHeader('Content-Disposition');
let filename = "archivos.zip";
if (disposition && disposition.indexOf('attachment') !== -1) {
const match = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(disposition);
if (match != null && match[1]) {
filename = match[1].replace(/['"]/g, '');
}
}
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
},
error: () => {
alertWarningMessage("Error al descargar el archivo ZIP.");
},
complete: () => {
$("#loader").modal('hide');
}
});
}
dropZoneUpdateFiles(files) {
files.forEach(file => {

View File

@ -58,6 +58,7 @@ class Resumen {
this.btnPreviewCubierta = $(this.domItem.find("#btnPreviewCubierta"));
this.btnDownloadPreviewCubierta = this.domItem.find('.download-shape');
this.submitFiles = $(this.domItem.find("#submit-all-files"));
this.btnDownloadAllFiles = this.domItem.find("#download-all-files");
this.dropzone = null;
this.presupuesto_id = -1;
@ -68,6 +69,7 @@ class Resumen {
this.btnPreviewCubierta.on('click', this.#btnPreview.bind(this));
this.submitFiles.on('click', this.#btnUploadFiles.bind(this));
this.btnDownloadAllFiles.on('click', this.#downloadAllFiles.bind(this));
this.downloadPreviewImage()
if (presupuesto_id != -1) {
this.presupuesto_id = presupuesto_id;
@ -186,6 +188,43 @@ class Resumen {
});
}
#downloadAllFiles() {
$('#loader').show();
$.ajax({
url: "/presupuestocliente/download_zip",
type: 'POST',
data: { presupuesto_id: this.presupuesto_id },
xhrFields: {
responseType: 'blob' // importante para recibir un archivo binario
},
success: function (blob, status, xhr) {
const disposition = xhr.getResponseHeader('Content-Disposition');
let filename = "archivos_presupuesto.zip";
if (disposition && disposition.indexOf('attachment') !== -1) {
const match = /filename[^;=\n]*=((['"]).*?\2|[^;\n]*)/.exec(disposition);
if (match != null && match[1]) filename = match[1].replace(/['"]/g, '');
}
const url = window.URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = filename;
document.body.appendChild(a);
a.click();
a.remove();
window.URL.revokeObjectURL(url);
},
error: function () {
popErrorAlert("Error al descargar el archivo ZIP");
},
complete: function () {
$('#loader').hide();
}
});
}
#btnPreview() {
@ -384,7 +423,7 @@ class Resumen {
const svgData = serializer.serializeToString(shapeSvgEl);
const svgBlob = new Blob([svgData], { type: 'image/svg+xml;charset=utf-8' });
const svgUrl = URL.createObjectURL(svgBlob);
const img = new Image(shapeSvgEl.getBoundingClientRect().width,shapeSvgEl.getBoundingClientRect().height);
const img = new Image(shapeSvgEl.getBoundingClientRect().width, shapeSvgEl.getBoundingClientRect().height);
img.onload = () => {
const scaleFactor = 3; // Increase resolution (e.g., 2x, 3x)
const canvas = document.createElement('canvas');
@ -395,7 +434,7 @@ class Resumen {
canvas.height = height * scaleFactor;
const ctx = canvas.getContext('2d');
ctx.scale(scaleFactor, scaleFactor);
ctx.drawImage(img, 0,0,width,height);
ctx.drawImage(img, 0, 0, width, height);
const pngUrl = canvas.toDataURL('image/png');
const downloadLink = document.createElement('a');
downloadLink.href = pngUrl;