1

i'm uploading images to gridfs-stream using node and express..uploading is working fine but am unable to download

  app.post('/upload', function (req, res) {
    var tempfile = req.files.displayImage.path;
    var origname = req.files.displayImage.name;
    var _id = guid();
    var writestream = gfs.createWriteStream({
        filename: _id
    });
    // open a stream to the temporary file created by Express...
    fs.createReadStream(tempfile)
        .on('end', function () {
            res.send(_id);
        })
        .on('error', function () {
            res.send('ERR');
        })
    // and pipe it to gfs
    .pipe(writestream);
});

app.get('/download', function (req, res) {
    // TODO: set proper mime type + filename, handle errors, etc...
    gfs
    // create a read stream from gfs...
    .createReadStream({
        filename: req.param('filename')
    })
    // and pipe it to Express' response
    .pipe(res);
});  

the above code is unable to download the image by this cmd download?filename=acf58ae4-c853-f9f3-5c66-c395b663298a

user3279499
  • 35
  • 1
  • 6

1 Answers1

0

You might need to check your values in params. But hopefully this near minimal sample provides some help:

Update

And it has helped, because it highlights that you are looking up the _id as a filename. Instead you should be doing this:

.createReadStream({
    _id: req.param('filename')
})

if not

.createReadStream({
    _id: mongoose.Types.ObjectId(req.param('filename'))
})

Since the _id field is different to the filename


app.js

var express = require('express');
var routes = require('./routes');
var http = require('http');
var path = require('path');

var app = express();

var mongoose = require('mongoose');
var Grid = require('gridfs-stream');
Grid.mongo = mongoose.mongo;

var conn = mongoose.createConnection('mongodb://localhost/mytest');

conn.once('open', function() {
    console.log('opened connection');
    gfs = Grid(conn.db);

    // all environments
    app.set('port', process.env.PORT || 3000);
    app.use(express.logger('dev'));
    app.use(app.router);

    // development only
    if ('development' == app.get('env')) {
        app.use(express.errorHandler());
    }

    app.get('/', routes.index);

    http.createServer(app).listen(app.get('port'), function(){
        console.log('Express server listening on port ' + app.get('port'));
    });

});

routes/index.js

exports.index = function(req, res){

    res.set('Content-Type', 'image/jpeg');
    gfs.createReadStream({
        filename: 'receptor.jpg'
    }).pipe(res);

};
Neil Lunn
  • 148,042
  • 36
  • 346
  • 317
  • Can I read the file by `new ObjectID()` that was set?. Thanks for the answer. Still it is not working :( – user3279499 Mar 14 '14 at 12:25
  • @user3279499 The problem is clear now, see the edit. – Neil Lunn Mar 14 '14 at 12:33
  • @user3279499 Your **acceptance** of the answer would be a nice thankyou :) – Neil Lunn Mar 14 '14 at 13:43
  • am getting at console like this...GET /download?filename=%22532305b8b0a858b820f34edd%22 500 2ms - 1.17kb...but when i try to view image by using localhost:8087/download?filename="532305b8b0a858b820f34edd"..unable to render it.. :( – user3279499 Mar 14 '14 at 13:45
  • @user3279499 There **should** be enough information here for you to debug or otherwise understand the problem. Please read through again and re-think what you are trying. I am happy to help but I am also very busy. I think the information in the **answer** is complete enough for you to fix your code. – Neil Lunn Mar 14 '14 at 13:54
  • getting a error like dis TypeError: Cannot call method 'ObjectID' of undefined – user3279499 Mar 14 '14 at 14:30