使用Connect上传文件

File uploading with Connect

本文关键字:文件 Connect 使用      更新时间:2023-09-26

我试图上传一个文件,并检查它与这个简单的代码。

var connect = require("connect")
    , http = require('http');
var app = connect()
    .use(connect.logger('dev'))
    .use(connect.static('static'))
    .use(connect.bodyParser())
    .use(function(req, res, next){
        if ('POST' == req.method) {
            console.log(req.body);
            console.log(req.body.file);
            res.writeHead(200);
            res.end('Uploaded');
        } else {
            next();
        }
    });
connect.createServer(app).listen(3000);

在静态文件夹中有index.html:

 <form action="/" method="POST" enctype="multipart/form-data">
     <input type="text" name="texten">
     <input type="file" name="displayImage">
     <button>Send file!</button>
 </form>
在浏览器中,我在文本字段中写入一些文本,为文件输入选择一个简单的.txt文件。然后按"提交"。

在控制台中,文本字段的内容按预期输出,但是找不到有关它的文件或信息。

Q: Where in req。正文文件的位置,以及我如何访问有关它的信息?

文件上传所需的多部分提交不由bodyparser处理。您将需要一些其他中间件,如connect-busboyconnect-multiparty

然后您可以从req.files对象访问上传的文件。

应该使用文件系统核心模块将正文的内容写入文件。