Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
131 views
in Technique[技术] by (71.8m points)

javascript - Upload files with node.js

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
odejsexpress.js:42:22 at callbacks (c:NodeInstall
odejs
ode_modulesexpresslib
outerindex.js:164:37) at param (c:NodeInstall
odejs
ode_modulesexpresslib
outerindex.js:138:11) at pass (c:NodeInstall
odejs
ode_modulesexpresslib
outerindex.js:145:5) at Router._dispatch (c:NodeInstall
odejs
ode_modulesexpresslib
outerindex.js:173:5) at Object.router (c:NodeInstall
odejs
ode_modulesexpresslib
outerindex.js:33:10) at next (c:NodeInstall
odejs
ode_modulesexpress
ode_modulesconnectlibproto.js:193:15) at Object.expressInit [as handle] (c:NodeInstall
odejs
ode_modulesexpresslibmiddleware.js:30:5) at next (c:NodeInstall
odejs
ode_modulesexpress
ode_modulesconnectlibproto.js:193:15) at Object.query [as handle] (c:NodeInstall
odejs
ode_modulesexpress
ode_modulesconnectlibmiddlewarequery.js:45:5)

What could be the reason?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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
})

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...