简体   繁体   中英

File upload using MeteorJS?

(Cross post from the Meteor forums )

Say I wanted to upload a file to a server that is built with Meteor from another computer through HTTP when the second computer hits a certain API.

So far, I've been able to create such an application using NodeJS, the Express framework, and multer middlewear. I thought it would be easy to move that to Meteor.

NodeJS:

var express = require('express');
var multer = require('multer');
var done = false;
var port = 8888;

var app = express();

//Multer configuration
app.use(multer({
    dest: './uploads/',
    limits: {
        fileSize: undefined
    },
    rename: function(fieldName, fileName){
        return fieldName + Date.now();
    },
    onFileUploadStart: function(file){
        console.log(file.originalname + ' has started downloading!');
    },
    onFileUploadComplete: function(file){
        console.log(file.fieldname + ' has been uploaded to ' + file.path);
        done = true;
    },
    onFileSizeLimit: function(file){
        console.log("File " + file.originalname + " is too large");
    },
    onError: function(){
        console.log("ERROR!!");
    }
}));

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

    if(done === true){
        console.log(req.files);
        done = false;
    }

    res.write("Ack!");
    res.end();
});

app.listen(port);

However, when I tried to translate the app to Meteor, files were just not uploaded.

MeteorJS

var multer = Npm.require('multer');

var done = false;

//Set up Multer
Picker.middleware(multer({
    dest: './fileUploads',
    limits: {
        fileSize: undefined
    },
    rename: function(fieldName, fileName){
        return fieldName + Date.now();
    },
    onFileUploadStart: function(file){
        console.log("[.]" + file.originalname + " upload has started at " + Date.now());
    },
    onFileUploadComplete: function(file){
        console.log("[.]" + file.originalname + " upload has finished at " + Date.now());
        done = true;
    },
    onFileSizeLimit: function(file){
        console.log("[.]" + file.originalname + " file size limit has been reached");
    },
    onError: function(){
        console.log("[.]ERROR!");
    }
}));

var postPicker = Picker.filter(function(req, res){
    return req.method === "POST";
});

//Actual route, after the middleware has been set up
postPicker.route('/api/upload', function(params, req, res, next){

    if(done === true){
        console.log(req.files);
        done = false;
    }

    res.write("Ack!");
    res.end();
})

When a call is made to the Meteor server, nothing happens. There is no error, and the server does not print anything. However, the caller receives the "Ack!" from the server.

This has me a bit baffled, and I will appreciate any and all help.

Is there a better way of doing this?

Thank you!

 FS.Utility.eachFile(event, function(file){ var doc = new FS.File(file); doc.metadata = {}; _.extend(doc.metadata, { userId: userId }); Images.insert(doc, function(err, fileObj){ }) }) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM