通过ID或文件名获取文件

时间:2019-04-04 17:40:55

标签: node.js file mongoose

通过其ID或其任何属性获取文件实际上不起作用不 知道为什么尝试了所有解决方案

    const Files = require('../../models/GALLERY');
const conn = mongoose.connection;
    Grid.mongo = mongoose.mongo;
    let gfs;

    conn.once("open", () => {
        gfs = Grid(conn.db);




        router.get('/home', (req, res) => {
            Files.find()
                .exec()
                .then(files => {
                    let uploadedFiles = files.map(file => ({
                        file_name: file.name,
                        file_id:file.doc_id,
                        file_type: file.type,
                        file_link: `http://${req.headers.host}/v1/bucket/download?document_id=${file.doc_id}`
                    }));
                    res.json({
                        success: true,
                        uploadedFiles
                    })
                })
                .catch(err => {
                    logger.error(`[*] Error, while getting all uploaded file, with error:  ${err}`);
                    res.status(400).send({
                        message: `Error, while getting all uploaded file, with error: ${err}`
                    });
                });
        });


        router.get('/bucket/download/:id', (req, res) => {
           const id=req.params.id
            gfs.findOne({_id: id }, (err, file) => {
                if (!file) {
                    return res.status(404).send({
                        message: 'File was not found'
                    });
                }
                let data = [];
                let readstream = gfs.createReadStream({
                    filename: file.filename
                });
                readstream.on('data', (chunk) => {
                    data.push(chunk);
                });
                readstream.on('end', () => {
                    data = Buffer.concat(data);
                    let type = fileType(data);
                    res.writeHead(200, {
                        'Content-Type': type.mime,
                        'Content-disposition': 'attachment; filename=' + file.filename + '.' + type.ext,
                        'Content-Length': file.length
                    });
                    res.end(data);
                });
                readstream.on('error', (err) => {
                    //logger.error(`[*] Error, while downloading a file, with error:  ${err}`);
                    res.status(400).send({
                        message: `Error, while downloading a file, with error:  ${err}`
                    });
                });
            });
        });

这是(图库)的模型

    const mongoose = require('mongoose');
const GALLERYSchema = new mongoose.Schema({
    doc_id: {
        type: String
    },
    length : {
        type: Number
    },
    name: {
        type: String
    },
    type: {
        type: String
    }
});

module.exports = mongoose.model('File', GALLERYSchema);

它总是转到(res.status(400).send({                             消息:Error, while downloading a file, with error: ${err}

或找不到蜂箱文件 如果有人可以帮助我,或者给我另一种获取文件的方式(图片) 要显示,甚至是bas64,这都很棒

0 个答案:

没有答案