使用node.js上传文件 [英] Upload files with node.js

查看:113
本文介绍了使用node.js上传文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  app.use(express.bodyParser()) ); 
//或者,`req.files`仅由多部分中间件提供,您可以
//添加只要您不关心解析非多部分上传,
// like:
app.use(express.multipart());

app.get('/',function(req,res){
fs.readFile('uploadHTML.html',function(err,data){
res。 writeHead(200,{'Content-Type':'text / html','Content-Length':data.length});
res.write(data);
res.end();
});

});
app.post('/ upload',function(req,res)
{
console.log(req.files);
fs.readFile(req.files.displayImage .path,function(err,data){
// ...
var newPath = __dirname;
fs.writeFile(newPath,data,function(err){
res .redirect(back);
});
});
});

这是HTML文件:

 < HTML> 
< head>
< title>上传示例< / title>
< / head>
< body>

< form id =uploadForm
enctype =multipart / form-data
action =/ upload
method =post>
< input type =fileid =userPhotoInputname =displayImage/>
< input type =submitvalue =Submit>
< / form>

< span id =status/>
< img id =uploadedImage/>


< / body>
< / html>

当我上传文件时,会给我下一个错误:

  TypeError:无法读取c:\NodeInstall\\\
odejs\express.js中未定义的属性displayImage:42:22在回调(c:\\参数中的\\NodeInstall\\\
odejs\\\
ode_modules\express\lib\router\index.js:164:37)(c:\NodeInstall\\\
odejs\\\
ode_modules\express\lib\router在路由器上传递(c:\NodeInstall\\\
odejs\\\
ode_modules\express\lib\router\index.js:145:5)(c:\index.js:138:11) Object.router中的\NodeInstall\\\
odejs\\\
ode_modules\express\lib\router\index.js:173:5)(c:\NodeInstall\\\
odejs\\\
ode_modules\express\lib \router\index.js:33:10)在下一个(c:\NodeInstall\\\
odejs\\\
ode_modules\express\\\
ode_modules\connect\lib\proto.js:193:15)at Object.expressInit [as handle] (c:\NodeInstall\\\
odejs\\\
ode_modules\express\lib\middleware.js:30:5)在下一个(c:\NodeInstall\\\
odejs\\\
ode_modules\express\\\
ode_modules\在Object.query [as handle](c:\NodeInstall\\\
odejs\\\
ode_modules\express\\\
ode_modules\connect\lib\middleware\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ \\query.js:45:5)

可能是什么原因?

解决方案

我建议您使用awesome模块 https://github.com/domharrington/fileupload 用于处理node / express中的文件上传。

  var fileupload = require('fileupload')。createFileUpload('/ uploadDir')。中间件

app.post('/ upload',fileupload,function(req,res){
// files现在在req.body对象以及其他表单域
//文件也得到移动到uploadDir指定
})


I have this code in order to upload files with node.js:

    app.use(express.bodyParser());
    // or, as `req.files` is only provided by the multipart middleware, you could 
    // add just that if you're not concerned with parsing non-multipart uploads, 
    // like:
    app.use(express.multipart());

    app.get('/',function(req,res){
    fs.readFile('uploadHTML.html',function (err, data){
        res.writeHead(200, {'Content-Type': 'text/html','Content-Length':data.length});
        res.write(data);
        res.end();
    });

    });
    app.post('/upload',function(req,res)
    {
    console.log(req.files);
    fs.readFile(req.files.displayImage.path, function (err, data) {
      // ...
      var newPath = __dirname;
      fs.writeFile(newPath, data, function (err) {
        res.redirect("back");
      });
    });
 });

Here is the HTML file:

<html>
<head>
<title>Upload Example</title>
</head>
<body>

<form id="uploadForm"
      enctype="multipart/form-data"
      action="/upload"
      method="post">
  <input type="file" id="userPhotoInput" name="displayImage" />
  <input type="submit" value="Submit">
</form>

<span id="status" />
<img id="uploadedImage" />


</body>
</html>

When I upload the file, it gives me the next error:

TypeError: Cannot read property 'displayImage' of undefined at c:\NodeInstall\nodejs\express.js:42:22 at callbacks (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:164:37) at param (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:138:11) at pass (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:145:5) at Router._dispatch (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:173:5) at Object.router (c:\NodeInstall\nodejs\node_modules\express\lib\router\index.js:33:10) at next (c:\NodeInstall\nodejs\node_modules\express\node_modules\connect\lib\proto.js:193:15) at Object.expressInit [as handle] (c:\NodeInstall\nodejs\node_modules\express\lib\middleware.js:30:5) at next (c:\NodeInstall\nodejs\node_modules\express\node_modules\connect\lib\proto.js:193:15) at Object.query [as handle] (c:\NodeInstall\nodejs\node_modules\express\node_modules\connect\lib\middleware\query.js:45:5)

What could be the reason?

解决方案

I do recommend you to use awesome module https://github.com/domharrington/fileupload for handling file uploads in node/express.

var fileupload = require('fileupload').createFileUpload('/uploadDir').middleware

app.post('/upload', fileupload, function(req, res) {
  // files are now in the req.body object along with other form fields
  // files also get moved to the uploadDir specified
})

这篇关于使用node.js上传文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆