0

I am trying to send a zip file from a node.js server to a node.js client but when i save the zip it is corrupted and will not open.

I am using adm-zip to zip the file and send to client

app.get('/checkForUpdate', function (req, res) {

    var zip = new AdmZip();
    zip.addLocalFile("./update.js");
    var willSendthis = zip.toBuffer();
    res.send(willSendthis);

});

here is my client code

$.ajax({
    type: 'GET',
    contentType: 'application/json',
    data: {version: version},
    url: 'http://localhost:3000/checkForUpdate',
    success: function (data) {

        fs.writeFile("update.zip", data, function(err) {

            if(err) {
                console.log(err);
            } else {
                console.log("The file was saved!");
            }

        });
    }
});

3 Answers 3

1

I guess this should be set for MIME type application/x-zip-compressed.

Sign up to request clarification or add additional context in comments.

1 Comment

Try these : application/zip, application/octet-stream
1

If you want to compress a text or any data as a zip file you can use JSZip npm package.

I have given the link below to install it.

https://www.npmjs.com/package/jszip

The code below converts the text file and sends it as a buffer that the client can download.

const zip = new JSZip()
zip.file("Hello.txt", "Hello World\n")
const buffer = await zip.generateAsync({ type: `nodebuffer` })
res.writeHead(200,{ 'Content-Type': `application/zip` })
res.end(buffer)

Comments

0

I think the issue is sending a buffer. If I remember right a buffer needs a mime type of octect-stream. Try using octect-stream in the header and have adm-zip load the buffer

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.