18

如何在 Node 中命名由 Formidable 解析的文件?目前我的文件保存为“7c9d182a9892bb7588b49ea9d364efd0.pdf”,我更喜欢原始文件名。

有人知道怎么做吗?

4

4 回答 4

27

我就是这样做的(查看 .on(file'...) 函数):

// import required modules
    var express = require('express');
    var formidable = require("formidable"); // used for parsing form data
    var fs = require('fs');

// prevent bodyParser from handling multipart forms (ie only handle get and post requests)
    delete express.bodyParser.parse['multipart/form-data'];

// create server
    var app = express.createServer();

// connect middleware
    app.configure(function() {
        // creates body object on request object
            app.use(express.bodyParser());
            app.use(express.methodOverride());
        // used to display static files such as css
            app.use(express.static(__dirname + '/static'));
    });

// enable and define views
    app.set('views', __dirname + '/views');
    app.set('view engine', 'jade'); 

//******************************
//* create routes
//******************************

// file upload handling
    app.get('/photos/new', function(request, response) {
        response.render('photos/new');
    });
    app.post('/photos', function(request, response) {
        console.log('in /photos handler');
        var form = new formidable.IncomingForm();

        form.uploadDir = __dirname + '/static/upload/photos/';

        form.on('file', function(field, file) {
            //rename the incoming file to the file's name
                fs.rename(file.path, form.uploadDir + "/" + file.name);
        });

        form.on('error', function(err) {
            console.log("an error has occured with form upload");
            console.log(err);
            request.resume();
        });

        form.on('aborted', function(err) {
            console.log("user aborted upload");
        });

        form.on('end', function() {
            console.log('-> upload done');
        });

        form.parse(request, function() {
                response.render('photos/new');
        });
    });

// start our server
    app.listen(8888);
于 2012-01-08T01:10:44.893 回答
20

这是我自己的答案:

form.uploadDir = "temp/uploads";
form.keepExtensions = true;

form
    .on('error', function(err) {
        throw err;
    })

    .on('field', function(field, value) {
        //receive form fields here
    })

    /* this is where the renaming happens */
    .on ('fileBegin', function(name, file){
            //rename the incoming file to the file's name
            file.path = form.uploadDir + "/" + file.name;
    })

    .on('file', function(field, file) {
        //On file received
    })

    .on('progress', function(bytesReceived, bytesExpected) {
        //self.emit('progess', bytesReceived, bytesExpected)

        var percent = (bytesReceived / bytesExpected * 100) | 0;
        process.stdout.write('Uploading: %' + percent + '\r');
    })

    .on('end', function() {


    });

form.parse(req);   

答案一直在模块的 GitHub 页面上!

于 2011-12-09T16:24:46.997 回答
1

您可以从强大的文件对象中获取原始名称:

form.parse(req, function(err, fields, files) {
  var filename = files.upload.name;
});

希望这可以帮助

于 2011-12-03T18:36:52.363 回答
1

只是想添加以供将来参考,似乎有一个提交添加了该选项:

var form = new formidable.IncomingForm();
form.keepFilenames = true;  

https://github.com/felixge/node-formidable/pull/488

虽然这看起来还没有在 npmjs.org 上发布,但我相信它会在提交被批准时发布。

于 2018-11-26T15:11:47.963 回答