angularjs 将上传的文件存储到Express Js中的文件系统

tf7tbtn2  于 2022-12-22  发布在  Angular
关注(0)|答案(3)|浏览(145)

我是nodejs的新手,我想知道如何将一个文件放到我的系统后端,甚至上传到S3等等。
下面是文件对象:

req.body { homeColor: 'black',
  guestColor: 'white',
  thirdColor: 'red',
  file: 
   { webkitRelativePath: '',
     lastModifiedDate: '2014-05-05T02:26:11.000Z',
     name: '2014-05-05 10.26.11.jpg',
     type: 'image/jpeg',
     size: 1310720 },
  title: 'tfuyiboinini' }

如何处理req.body.file,使其可以物理保存?
请帮忙,谢谢!

xpcnnkqh

xpcnnkqh1#

长话短说

var fs = require('fs');
app.post('/file-upload', function(req, res) {
    var tmp_path = req.files.thumbnail.path;
    var target_path = './public/images/' + req.files.thumbnail.name;
    fs.rename(tmp_path, target_path, function(err) {
        if (err) throw err;
        fs.unlink(tmp_path, function() {
            if (err) throw err;
            res.send('File uploaded to: ' + target_path + ' - ' + req.files.thumbnail.size + ' bytes');
        });
   });
};

您可以在这里阅读更多信息:http://www.hacksparrow.com/handle-file-uploads-in-express-node-js.html

os8fio9y

os8fio9y2#

首先确保POST编码为enctype="multipart/form-data"....
在Express 4中,您需要在服务器中设置body parser

var bodyParser     = require('dy-parser');
//...

var app            = express();
//...
app.use(bodyParser());  // pull information from html in POST

var busboy = require('connect-busboy');    
app.use(busboy());

在早期版本的Express中,您只需要从框架本身添加主体解析器,文件将存储在配置的位置:

app.use(express.bodyParser({limit: '10mb', uploadDir: __dirname + '/public/uploads' }));               // pull information from html in POST

由于版本4删除了对连接的支持,现在您需要添加对多部分/表单数据的自定义支持来解析多部分/部分POST,因此您必须执行以下操作:

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

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

qxgroojn3#

topic已共享正确答案,因此可能会有帮助。
此外,请找到以下解决方案。

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

var app = express();
app.use(busboy());
app.use(express.static(path.join(__dirname, 'public')));

app.route('/fileupload')
    .post(function (req, res, next) {

        var fstream;
        req.pipe(req.busboy);
        req.busboy.on('file', function (fieldname, file, filename) {
            console.log("Uploading: " + filename);

            //Path where image will be uploaded
            fstream = fs.createWriteStream(__dirname + '/img/' + filename);
            file.pipe(fstream);
            fstream.on('close', function () {    
                console.log("Uploading process finish " + filename);              
               // res.redirect('back');
            });
        });
    });

相关问题