Reputation: 86
I am new to node.js .And need to upload an image when an api is called,the image is given as multipart form data.
How could i get the image details as the response?
Upvotes: 0
Views: 1000
Reputation: 1742
You can use multer.js as the body-parser. Its built on top of busboy. Its quite well-documented .
Upvotes: 1
Reputation: 106
The answer of this question is as follow :
fileupload.js :
var express = require("express"),
app = express();
// tell express to use the bodyParser middleware
// and set upload directory
app.use(express.bodyParser({ keepExtensions: true, uploadDir: "uploads" }));
app.engine('jade', require('jade').__express);
app.post("/upload", function (request, response) {
// request.files will contain the uploaded file(s),
// keyed by the input name (in this case, "file")
// show the uploaded file name
console.log("file name", request.files.file.name);
console.log("file path", request.files.file.path);
response.end("upload complete");
});
// render file upload form
app.get("/", function (request, response) {
response.render("upload_form.jade");
});
app.listen(3000);
View :
doctype 5
html
head
title Upload Form
body
h1 Upload File
form(method="POST", action="/upload", enctype="multipart/form-data")
input(type="file", name="file")
input(type="submit")
Upvotes: 0