Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
358 views
in Technique[技术] by (71.8m points)

javascript - Changing a buffer from a ReadStream into an actual file

So I have this code:

downloadFile(file_id) {
    return new Promise((resolve, reject) => {
        var mongoose = require('mongoose');
        var Grid = require('gridfs-stream');
        var fs = require('fs');

        mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
        var conn = mongoose.connection;
        Grid.mongo = mongoose.mongo;
        var gfs = Grid(conn.db);
        console.log('downloadfile', file_id);
        var read_stream = gfs.createReadStream({_id: file_id});
        let file = [];
        read_stream.on('data', function (chunk) {
            file.push(chunk);
        });
        read_stream.on('error', e => {
            console.log(e);
            reject(e);
        });
        return read_stream.on('end', function () {
            resolve(file);
        });
    });
}

And I'm calling it like this:

Account.findById(req.params._id)
    .then(async account => {
        const file = await functions.downloadFile(account.employer.logo);
        res.render('users/employer/booth', {
            title: 'Employer Booth',
            user: req.user,
            postings: postings,
            employer: account.employer,
            event: event,
            logo: logo,
        });
    });

How can I either save this file to the server and serve it as an image, or just send it to the view and show it in an <img> tag?

I tried doing something like this:

read_stream.on('open', function () {
    var imgFile = fs.createWriteStream(`/public/images/logos/${file_id}.jpg`);
    var write_stream = read_stream.pipe(imgFile);
    read_stream.pipe(res);
});
read_stream.on('error', e => {
    console.log(e);
});

But this wasn't able to read the saved file, and the file wasn't saved anywhere.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Ok this saved my life: https://youtu.be/pXHOF4GWuZQ. This is the final code:

downloadFile(file_id) {
    return new Promise((resolve, reject) => {
        var mongoose = require('mongoose');
        var Grid = require('gridfs-stream');
        var fs = require('fs');

        mongoose.connect(config.db, {useNewUrlParser: true},).catch(e => console.log(e));
        var conn = mongoose.connection;
        Grid.mongo = mongoose.mongo;
        var gfs = Grid(conn.db);
        console.log('downloadfile', file_id);
        var read_stream = gfs.createReadStream({_id: file_id});
        let file = [];
        read_stream.on('data', function (chunk) {
            file.push(chunk);
        });
        read_stream.on('error', e => {
            console.log(e);
            reject(e);
        });
        return read_stream.on('end', function () {
            file = Buffer.concat(file);
            const img = `data:image/png;base64,${Buffer(file).toString('base64')}`;
            resolve(img);
        });
    });
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...