Varun Kumar Varun Kumar - 1 year ago 93
Node.js Question

File upload using Multipart fails in nodejs

I'm trying to upload a file in node js using multipart where i get Cannot POST error? I'm totally new to node js so can you help me what i'm doing wrong
My Code

<form id = "uploadForm"
enctype = "multipart/form-data"
action = "/api/uploadfile"
method = "post">
<input type="file" name="fileUpload"/>
<input type="submit" value="Upload File" name="submit">


var express = require('express');
var app = express();
var multer = require('multer');
app.get('/', function(request, response){

var storage = multer.diskStorage({
destination: function (req, file, callback) {
callback(null, './uploads');
filename: function (req, file, callback) {
callback(null, file.fieldname + '-' +;
var upload = multer({ storage : storage}).single('fileUpload');'/api/uploadfile',function(req,res){
upload(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
res.end("File is uploaded");

console.log("App listening on port 8080");

Error message as follows :

Error uploading file

Answer Source

i tried your code,its working here.The reason may be,

1)you missed out the closing of form tag

<form id       =  "uploadForm"
     enctype   =  "multipart/form-data"
     action    =  "/api/uploadfile"
     method    =  "post"
<input type="file" name="fileupload"  /> 
<input type="submit" value="Upload file" name="submit">

2)make sure that you have a folder named -> uploads