将文件从客户端浏览器上传到 mongodb 或磁盘?

Upload the files from the client browser into mongodb or disk?

我在使用 node.js 和 express 框架上传文件时遇到问题。

下面是我的 app.js 代码:

var express = require('express');
var fs = require('fs');
var busboy = require('connect-busboy');

var app = express();

app.use(busboy());

app.post('/fileupload', function(req, res) {
    var fstream;
    console.log(req.filename);
    req.pipe(req.busboy);   
    req.busboy.on('file', function (fieldname, file, filename) {
        console.log("Uploading: " + filename); 
        fstream = fs.createWriteStream(__dirname + '/public/' + filename);
        file.pipe(fstream);
        fstream.on('close', function () {
            res.redirect('back');
        });
    });
});

HTML代码为

<form id="uploadForm" enctype="multipart/form-data" action="/fileupload" method="post">
    <div class="azureD" style="display:none;">
        <div class="pull-left">
            <label class="labelTemp">Subscription ID</label>
            <div class="clickRole addStoTabWid">
                <input type="text" id="" placeholder="" style="border:none;width:100%;">    
            </div>
        </div>
        <div class="pull-left">
            <label class="labelTemp">Upload .pem file</label>
            <div class="clickRole addStoTabWid">
                <input type="file" name="file" id="file" placeholder="" style="border:none;width:100%;">
            </div>
        </div>
        <div class="modal-footer">
        </br>
        <input type="submit" value="Upload" name="submit">
    </div>
</form>

我在 node.js 控制台中收到休闲错误

TypeError: Cannot read property 'on' of undefined
at IncomingMessage.Readable.pipe (_stream_readable.js:495:7)
at C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\app.js:138:9
at callbacks (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:161:37)
at param (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:135:11)
at pass (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:142:5)
at Router._dispatch (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:170:5)
at Object.router (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\lib\router\index.js:33:10)
at next (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\node_modules\connect\lib\proto.js:190:15)
at Object.methodOverride [as handle] (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\node_modules\connect\lib\middleware\methodOverride.js:37:5)
at next (C:\Users\sangamesh.b\Desktop\release-2\Rapid_cloud\node_modules\express\node_modules\connect\lib\proto.js:190:15)
_stream_readable.js:505
dest.end();
    ^
TypeError: Cannot read property 'end' of undefined
at IncomingMessage.onend (_stream_readable.js:505:9)
at IncomingMessage.g (events.js:199:16)
at IncomingMessage.emit (events.js:129:20)
at _stream_readable.js:908:16
at process._tickDomainCallback (node.js:381:11)

我已经检查了我无法用我的代码解决这个问题的所有内容,请帮助我解决这个问题。

我想知道还有其他替代方法可以从浏览器上传文件并将其存储到 mongodb 或本地磁盘

看起来 fstream 未定义,因为 file.pipe() 导致错误。确保传递给 fs.createWriteStream()filepath 是正确的。

使用以下代码段。它适合你

var upload_path = path.resolve(__dirname + '../../../public/uploads');
var result = {
    status: 0,
    message: '',
    data: ''
};
fs.readFile(req.files.file.path, function (err, data) {

    var imageName = Date.now() +"_"+req.files.file.name;

    /// If there's an error
    if(err){
        //error
    } else {
        var newPath = path.resolve(upload_path, imageName);

        fs.writeFile(newPath, data, function (err) {
            if(err) {
               //error
            } else {
                fs.unlink(req.files.file.path, function() {
                    if (err) {
                        result.status = -1;
                        result.message = err;
                    } else {
                        result.data = imageName;
                    }
                    res.jsonp(result);
                });
            }
        });
    }
});