You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I am trying to create and send a zip file on click of the download button but instead of getting a .zip file, I am getting it in binary form in response. can anyone please help?
I am using Archiver npm package for compression.
Adonis version
4.1.1
Node.js and npm version
v18.16.0 and 9.5.1 respectively
Code:
sync createZip(export_value, params, response) {
if (export_value === 'export_model') {
const sourceFolder = `/${params.filePath.join('/')}`;
const isExist = await exists(sourceFolder);
if (isExist) {
const zipFilePath = `${sourceFolder}.zip`;
// Create a writable stream to the response
response.header('Content-Type', 'application/zip');
response.header('Content-Disposition', 'attachment; filename=final.zip');
// Create a new zip archive
const archive = archiver('zip', { zlib: { level: 9 } });
archive.pipe(response.response);
// Recursive function to add files to the zip archive
function addFilesToArchive(directory) {
const files = fs.readdirSync(directory);
files.forEach((file) => {
const filePath = path.join(directory, file);
const stat = fs.statSync(filePath);
if (stat.isFile()) {
// Add the file to the archive, provide some detection for images here
archive.file(filePath, { name: file });
} else if (stat.isDirectory()) {
// Recursively add files from subdirectories
addFilesToArchive(filePath);
}
});
}
// Start adding files from the specified directory
addFilesToArchive(sourceFolder);
// Finalize the archive and send it to the client
archive.on('end', () => {
console.log('Folder zipped successfully.');
})
await archive.finalize();
return true
}
}
}
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
Uh oh!
There was an error while loading. Please reload this page.
-
I am trying to create and send a zip file on click of the download button but instead of getting a .zip file, I am getting it in binary form in response. can anyone please help?
I am using Archiver npm package for compression.
Adonis version
4.1.1
Node.js and npm version
v18.16.0 and 9.5.1 respectively
Code:
Beta Was this translation helpful? Give feedback.
All reactions