Monday 9 September 2019

Picture breaks during download

When a file is uploaded, the image is breaks down and not displayed. Why is this happening and how to fix it?

console.log(file._props):

{ SYSFILE_ID: 396,
   FILE_CONTENT:
   '����\u0000\u0010JFIF\u0000\u0001\...etc.',
   PRESCRIPTIONS_ID: 287,
   NAME: '1472042903_31.2.jpg',
   COMMENTS: 'null',
   MIMETYPE: 'image/jpeg' }

Moreover, only images break, and text files always work.

controller:

module.exports.download = async function (req, res) {
  SysFiles.findById(req.params.SYSFILE_ID).then(file => {
    var fileContents = Buffer.from(file._props.FILE_CONTENT, "base64");
    var readStream = new stream.PassThrough();
    readStream.end(fileContents);
    res.set('Content-disposition', 'attachment; filename=' + encodeURIComponent(file._props.NAME));
    res.set('Content-Type', file._props.MIMETYPE);
    readStream.pipe(res);
  })
};

Result:

file 1472042903_31.2.jpg

1472042903_31.2.jpg: data



from Picture breaks during download

No comments:

Post a Comment