Safari failed to download pdf file from server

CODE

download: function (req, res) {
        var id = req.param('id');
        if (!id) {
            return res.notFound();
        }

        var Grid = require('gridfs-stream');
        var fs = require('fs');

        var grid = new Grid(gridDb, mongo);

        Document.findOneById(id.trim()).exec(function (err, doc) {
            if (err) {
                return res.serverError(err);
            }

            if (!doc) {
                return res.notFound();
            }

            grid.collection('document').findOne({idStr: doc.id}, function (err, file) {
                if (err) {
                    return res.serverError(err);
                }

                if (file) {
                    if (doc.contentType = "application/pdf") {
                        res.set({
                            'Content-Type': 'application/octet-stream',
                            'Content-Disposition': 'attachment; filename=' + doc.filename,
                            'Content-Length': file["length"]
                        });
                    } else {
                        res.set({
                            'Content-Type': file.contentType,
                            'Cache-Control': 'public',
                            'Pragma': 'public',
                            'ETag': file['md5'],
                            'Accept-Ranges': 'bytes',
                            'Content-Length': file["length"]
                        });
                    }
                    sails.log("re")
                    var readstream = grid.createReadStream({_id: id, root: 'document'});
                    return readstream.pipe(res);

                } else {
                    return res.notFound();
                }
            });
        });

      

PROBLEM

This code loads a file from gridFs. It works great in all browsers for all file types except safari pdf file. Can anyone tell me what is wrong with the code, or maybe it's a Safari bug? I also tried the following code:

res.set({
   'Content-Disposition': 'attachment; filename=' + doc.filename,
   'Content-Type': 'application/pdf',
   'Content-Length': file["length"],
   'Content-Transfer-Encoding': 'binary',
   'Cache-Control': 'public',
   'Pragma': 'public',
   'ETag': file['md5']
});

      

+3


source to share





All Articles