IPhone uploads binary to Node.js server (using express, mongoDB and mongoose)

sorry, very new to Node.js here. I'm having trouble with a good strategy for uploading files from an iphone client to a Node.js server and storing it on the server side.

Now I can accept the binary on the server and save it to the filesystem with the following code:

app.post('/upload', function(req, res){

    // get the temporary location of the file
    var tmp_path = req.files.pic.path;
    // set where the file should actually exists - in this case it is in the "images" directory
    var target_path = './uploads/' + req.files.pic.name;
    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
        fs.unlink(tmp_path, function() {
            if (err) throw err;
            res.send('File uploaded to: ' + target_path + ' - ' + req.files.pic.size + ' bytes');
        });
    });

    console.log(req.files.pic.name);
    res.send('DONE', 200);
    res.end();
});

      

With this code, I first accept multi-page jpeg download from iphone to / tmp directory and then rename and move the file to directory. / uploads. My problem is how to save this file to the DB.

From what I've read, I have three options (I think!)

  • Save the files to some downloadable directory and save the local path to mongodb for later access.
  • Save the file itself to MongoDb with a buffer
  • Use grid-fs to save the file.

I'm trying # 3 to use this module I found is called gridfs-stream (since I'm using mongoose), but I don't really understand the source code in the package.

My question is bipartisan: among the 3 options above, would there really be a 3rd way? If so, I really need help understanding how to use the gridfs stream.

I know the following code is wrong, but for now this is my attempt to see if I can fold it into the existing load code:

app.post('/upload', function(req, res){

    // get the temporary location of the file
    var tmp_path = req.files.pic.path;
    // set where the file should actually exists - in this case it is in the "images" directory
    var target_path = './uploads/' + req.files.pic.name;
    // move the file from the temporary location to the intended location
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;

        var conn = mongoose.createConnection('localhost', 'myahkvoicedb');

        conn.once('open', function () {
            var gfs = Grid(conn.db, mongoose.mongo);

            // all set!
            var writestream = gfs.createWriteStream('req.files.pic.name');
            fs.createReadStream('./uploads/').pipe(writestream);

            /* // APP CRASHES HERE WITH THE FOLLOWING:
            stream.js:81
            throw er; // Unhandled stream error in pipe.
            ^
            Error: EISDIR, read
            */
        })


        // delete the temporary file, so that the explicitly set temporary upload dir does not get filled with unwanted files
        fs.unlink(tmp_path, function() {
            if (err) throw err;
            res.send('File uploaded to: ' + target_path + ' - ' + req.files.pic.size + ' bytes');
        });
    });

    console.log(req.files.pic.name);
    res.send('DONE', 200);
    res.end();
});

      

Any help would be greatly appreciated. Thank!

+3


source to share





All Articles