admin管理员组

文章数量:1023213

I'm trying to make an API to upload file using Node.js server. I get undefined response.

I am following this tutorial

Node.js:

var express = require('express');

var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.post("*", function(req, res) {
  res.end(JSON.stringify(req.files) + "\n");
});

console.log("Server at 8080");
app.listen(8080);

HTML

<html>
  <head>
    <form method="post"
          enctype="multipart/form-data"
          action="http://localhost:8080">
      <input type="file" name="myimage" />
      <input type="submit" name="submit" value="submit"/>
    </form>
  </head>
</html>

After clicking submit I got undefined response.

I'm trying to make an API to upload file using Node.js server. I get undefined response.

I am following this tutorial https://www.youtube./watch?v=UtfZ-5WKpro

Node.js:

var express = require('express');

var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.post("*", function(req, res) {
  res.end(JSON.stringify(req.files) + "\n");
});

console.log("Server at 8080");
app.listen(8080);

HTML

<html>
  <head>
    <form method="post"
          enctype="multipart/form-data"
          action="http://localhost:8080">
      <input type="file" name="myimage" />
      <input type="submit" name="submit" value="submit"/>
    </form>
  </head>
</html>

After clicking submit I got undefined response.

Share Improve this question edited Nov 8, 2016 at 10:21 Nhan 3,9056 gold badges34 silver badges40 bronze badges asked Nov 8, 2016 at 9:33 Abhishek ParikhAbhishek Parikh 9891 gold badge22 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
bodyParser.json()

… so you've set up a parser for JSON formatted requests

 enctype="multipart/form-data"

… but you aren't making a JSON formatted request.

See the documentation for body-parser:

This does not handle multipart bodies, due to their plex and typically large nature. For multipart bodies, you may be interested in the following modules:

… which is followed by a list of suggestions.

Pick a module which can handle multipart requests and use that instead of your current choice.

I remend you to use this module for handling file upload 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
});

Another way to upload files could be using something like this

Jade template

form.data(action='/user/register', method='post', class="long-fields", enctype='multipart/form-data')
  input(type="text" name="name")
  input(name='fileLogo', type='file')
  input(type="submit" value="Register")

Controller

formidable = require('formidable'); //file upload handling via form
uuid = require('node-uuid'); //Unique ID
path = require('path'); //Path piler
fs = require('fs'); //FileSystem

var form = new formidable.IningForm();
form.keepExtensions = false;
form.maxFieldsSize = 2 * 1024 * 1024; //2mb

form.parse(req, function(err, fields, files) {

  console.log(fields);
  console.log(files);

  fs.readFile(files.fileLogo.path, function (err, data) {
    var pathNew = __dirname + '/../../uploads/' + uuid.v1() + path.extname(files.fileLogo.name);

    fs.writeFile(pathNew, data, function (err) {
      console.log('uploaded', pathNew);
    });
  });

  res.send(jade.renderFile(settings.pathLess + prefix + '/register.jade', {
    req: req
  }));

});

I'm trying to make an API to upload file using Node.js server. I get undefined response.

I am following this tutorial

Node.js:

var express = require('express');

var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.post("*", function(req, res) {
  res.end(JSON.stringify(req.files) + "\n");
});

console.log("Server at 8080");
app.listen(8080);

HTML

<html>
  <head>
    <form method="post"
          enctype="multipart/form-data"
          action="http://localhost:8080">
      <input type="file" name="myimage" />
      <input type="submit" name="submit" value="submit"/>
    </form>
  </head>
</html>

After clicking submit I got undefined response.

I'm trying to make an API to upload file using Node.js server. I get undefined response.

I am following this tutorial https://www.youtube./watch?v=UtfZ-5WKpro

Node.js:

var express = require('express');

var app = express();
var bodyParser = require('body-parser');

app.use(bodyParser.json());

app.post("*", function(req, res) {
  res.end(JSON.stringify(req.files) + "\n");
});

console.log("Server at 8080");
app.listen(8080);

HTML

<html>
  <head>
    <form method="post"
          enctype="multipart/form-data"
          action="http://localhost:8080">
      <input type="file" name="myimage" />
      <input type="submit" name="submit" value="submit"/>
    </form>
  </head>
</html>

After clicking submit I got undefined response.

Share Improve this question edited Nov 8, 2016 at 10:21 Nhan 3,9056 gold badges34 silver badges40 bronze badges asked Nov 8, 2016 at 9:33 Abhishek ParikhAbhishek Parikh 9891 gold badge22 silver badges42 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 5
bodyParser.json()

… so you've set up a parser for JSON formatted requests

 enctype="multipart/form-data"

… but you aren't making a JSON formatted request.

See the documentation for body-parser:

This does not handle multipart bodies, due to their plex and typically large nature. For multipart bodies, you may be interested in the following modules:

… which is followed by a list of suggestions.

Pick a module which can handle multipart requests and use that instead of your current choice.

I remend you to use this module for handling file upload 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
});

Another way to upload files could be using something like this

Jade template

form.data(action='/user/register', method='post', class="long-fields", enctype='multipart/form-data')
  input(type="text" name="name")
  input(name='fileLogo', type='file')
  input(type="submit" value="Register")

Controller

formidable = require('formidable'); //file upload handling via form
uuid = require('node-uuid'); //Unique ID
path = require('path'); //Path piler
fs = require('fs'); //FileSystem

var form = new formidable.IningForm();
form.keepExtensions = false;
form.maxFieldsSize = 2 * 1024 * 1024; //2mb

form.parse(req, function(err, fields, files) {

  console.log(fields);
  console.log(files);

  fs.readFile(files.fileLogo.path, function (err, data) {
    var pathNew = __dirname + '/../../uploads/' + uuid.v1() + path.extname(files.fileLogo.name);

    fs.writeFile(pathNew, data, function (err) {
      console.log('uploaded', pathNew);
    });
  });

  res.send(jade.renderFile(settings.pathLess + prefix + '/register.jade', {
    req: req
  }));

});

本文标签: javascriptUpload file to Nodejs with HTMLStack Overflow