Two simple questions:
Is there a way I can:
- set a file size limit for the Gantt export, and
- know prior to exporting how big the file would be?
Thanks in advance.
Two simple questions:
Is there a way I can:
Thanks in advance.
Hello Nikolai,
There’s no way to determine the file size before sending a request to the export server, because the image is generated on their side after the full Gantt data (HTML, styles, etc.) is sent.
However, you can get the direct link to the exported file. From there, you can send a HEAD
request to get its Content-Length
and check the actual file size. Then, based on that, you can decide whether to download it:
function exportToPNG() {
const maxSizeBytes = 1 * 1024 * 1024; // 1 MB
gantt.exportToPNG({
raw: true,
callback: (result) => {
const fileURL = result.url;
const fileName = fileURL.split("/").reverse()[0];
fetch(fileURL, { method: 'HEAD' })
.then(response => {
const size = parseInt(response.headers.get('Content-Length'));
const sizeMb = (size / (1024 * 1024)).toFixed(2);
if (size > maxSizeBytes) {
alert(`File is too large: ${sizeMb} MB (limit is 1 MB)`);
return;
}
if (confirm(`File size: ${sizeMb} MB\nDownload file?`)) {
const link = document.createElement('a');
link.href = fileURL;
link.download = fileName;
document.body.appendChild(link);
link.click();
document.body.removeChild(link);
}
})
.catch(() => alert('Error getting file size'));
}
});
}
Here is an example: DHTMLX Snippet Tool