My goal is to attach a zip file to an object and request the zip file when the object is called from the client. I was successful in uploading the zip file using the following code:
const upload = multer({
fileFilter(req, file, cb){
if (!file.originalname.match(/\.(html|zip)$/)) {
return cb(new Error("Please upload a .html or .zip file"))
}
cb(undefined, true)
}
})
router.post('/map/:id/file/upload/zip', auth, upload.single('zip'), async(req, res) => {
try {
const map = await Map.findOne({_id:req.params.id})
if (!map) {
return res.status(400).send({"message":"Map is not found in database"})
}
map.zipBuffer = req.file.buffer
map.save()
res.status(201).send(map)
} catch (e) {
res.status(500).send(e)
}
})
Although the file saved successfully, when I tried to download it, I encountered the following error:
The archive is either in unknown format or damaged
Here is the code block I used to pull the zip file:
router.get('/map/:id/file/upload/zip', async(req,res) => {
try {
const map = await Map.findOne({_id:req.params.id})
if (!map) {
return res.status(400).send({"message":"No map found"})
}
const html = map.htmlBuffer
res.set("Content-Type","application/x-zip-compressed")
res.send(html)
} catch (e) {
res.status(500).send(e)
}
})
I am seeking assistance on how to properly upload a zipped file and retrieve it without damaging the file.