Node.js uploaded file does not have path property - node.js

I am writing a file upload demo by multer in node.js.When form submit, I can get the file information, but it does not have path property.
ejs file
<form method="post" enctype="multipart/form-data">
<p>
<input type="text" name="name" placeholder="name">
</p>
<p>
<input type="file" name="photo">
</p>
<p>
<input type="submit" value="Upload">
</p>
</form>
js
function (req, res ,next) {
var img = req.file;
var name = req.body.name || img.originalname;
var path = require('path').join(dir, name);
console.log(img);
}
multer config
var multer = require('multer')();
app.post('/upload',multer.single('photo'),photos.submit(app.get('photos')));
req.file

You are using memStorage for storing file. That's why there is no path in req.file. Try changing this to: var upload = multer({ dest: 'uploads/' }). Replace uploads/ with directory where you want to upload your file.

Related

NodeJS : Multer not saving file

I am working on a project where I need to save a file in a folder,
Here is my relevant code :
<div class="content ui segment" id="two">
<form method="POST" action = "/documents/uploadDoc" enctype="multipart/form-data">
<h2>Upload new file</h2>
<br>
<input class='button' type="file" id="myFile" name="filename">
<input type="submit" name = "Submit">
</form>
</div>
My multer code :
var multer = require('multer');
const fileStorage = multer.diskStorage({
destination : (req,file,cb) => {
cb(null,'tempSave/');
},
filename : (req,file,cb) => {
cb(null,file.originalname)
}
})
const saveFile = multer({ storage : fileStorage});
And my router :
router.post("/uploadDoc", saveFile.single('filename') ,function(req,res,next){
console.log("Uploading");
console.log(req.body);
var fileName = req.body.filename;
console.log(fileName);
});
There isn't any error, but the file is not getting saved in the required place. Please help
Can you check do you have tempSave folder in the root of your server directory? If not, create it and try it again.

file upload not working in express js using ejs view engine

I have tried to do file upload in express js project. used ejs as default view engine.
file not uploaded to /uploads folder.
here is my code below.
in app.js file have imported required packages
in routes/index.js route file created route
router.get('/index',indexController.index);
router.post('/insertion',indexController.insertion);
module.exports = router;
in controllers/indexController.js
const multer = require('multer');
const path = require('path');
const storage = multer.diskStorage({
destination:function(req,file,cb)
{
cb(null,'./../uploads');
},
filename:function(req,file,cb)
{
callback(null,file.originalname);
}
});
var upload = multer({storage:storage}).single('resume');
exports.insertion = function(req,res)
{
var file = req.files.resume;
upload(req,res,function(err){
if(err)
{
console.log(err);
}else{
console.log('file uploaded successfully!');
}
});
res.end();
}
in views/registration.ejs file
<form name="registration" action="/insertion" enctype="multipart/form-data" method="POST">
<div class="form-group">
<label>Name</label>
<input type="text" class="form-control" name="name">
</div>
<div class="form-group">
<label>Resume</label>
<input type="file" name="resume" class="form-control" >
</div>
<div class="form-group">
<input type="submit" class="btn btn-primary" name="submit" value="Submit">
</div>
</form>
file uploading not working .. i need to upload the file under uploads folder which is created in root directory

app.post and the express-fileupload dependency is producing no response

I have a simple html uploading form
<h1>Upload File Here</h1>
<form methods="post" enctype="multipart/form-data" action="/">
<input type="file" name="filename" /> <input type="submit" value="upload" />
</form>
I want uploaded files to hit a "/upload" folder. I am using the following express code to do this.
const express = require("express");
const app = express();
upload = require("express-fileupload");
app.listen(80);
app.use(upload());
console.log("Server Started");
app.get("/", (req, res) => {
res.sendFile(__dirname + "/index.html");
});
app.post("/", (req, res) => {
if (req.files) {
var file = req.files.filename,
filename = file.name;
file.mv("/upload/" + filename, err => {
if (err) {
console.log(err);
res.send("error occured");
} else {
res.send("done!");
}
});
}
});
When I start the server and go to localhost in Chrome, I see the upload form. If I try to upload something, the url box changes to reflect the file I tried to upload, but the file does not appear in the '/upload' folder. Any ideas on what obvious mistake I'm making??
Thanks!
The solution is in my form. I need to set the action="http://localhost:80/upload"
New HTML is
<h1>Upload File Here</h1>
<form
ref="uploadForm"
id="uploadForm"
action="http://localhost:80/upload"
method="post"
enctype="multipart/form-data"
>
<input type="file" name="filename" /> <input type="submit" value="upload" />
</form>
The solution is in my form. I need to set the
action="http://localhost:80/upload"
New HTML is
<h1>Upload File Here</h1>
<form
ref="uploadForm"
id="uploadForm"
action="http://localhost:80/upload"
method="post"
enctype="multipart/form-data"
>
<input type="file" name="filename" /> <input type="submit" value="upload" />
</form>

Formidable always returns empty fields and files when I upload a file in Node.js

Basically I want to upload a csv file from local computer and parse it in the backend to do required operations. I'm attaching the csv file in the front end. Checked that its not empty. But I'm unable to fetch the same in the server.
Is there something I'm missing or doing in the wrong way?
Here is what I have tried till now.
Front end code:
<form id="myForm" method="POST" enctype="multipart/form-data" action='/testcsv' >
<input type="file" id="file" />
<input type="submit" value="Submit">
</form>
Backend Code:
var express = require('express');
var methodOverride = require('method-override');
var http = require('follow-redirects').http;
var formidable = require('formidable');
var app = express();
const fs = require('fs');
app.use(methodOverride('_method'));
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // support json encoded bodies
app.use(bodyParser.urlencoded({ extended: true })); // support encoded bodies
app.post('/testcsv', requireLogin, function(req, res) {
var form = new formidable.IncomingForm();
form.parse(req, function(err, fields, files) {
console.log(err);
console.log(fields);
console.log(files);
});
});
Log Output:
null
{}
{}
This problem is caused by Frontend code, it has nothing to do with Backend code (formidable).
For the following console.log statement:
console.log(err);
console.log(fields);
console.log(files);
err is null because there is no error.
fields is {} because in the form, all input fields are file selector. fields in formidable only indicate plain input field, such as <input type="text">.
files is {} because the name of file selector in the form is missing.
To get expected value, here is an example form:
<form id="myForm" method="POST" enctype="multipart/form-data" action='/testcsv' >
<input type="text" name="testtext" />
<input type="file" id="file" name="testfile" />
<input type="submit" value="Submit">
</form>
The console.log result for above form would be:
null
{ testtext: '....' }
{ testfile: File....}

req.file is undefined using multer with upload.array to upload an image

Can someone please tell me what's wrong in my below code? I've been debugging this for hours but no luck.
var multer = require('multer')
var upload = multer({ dest: '/uploads/' })
router.post('/image', upload.array('file', 12) , function(req, res) {
console.log(req.body, req.file);
// I got this here {} undefined
});
cilent
<form method="post" action="/products/image" enctype="multipart/form-data">
<input type="file" name="file">
<input type="submit" value="Upload">
</form>
I noticed that on the server you use /image but on the client /products/image. It could be the problem, if not - please share all your server source code.
try to use req.files with 's', instead of req.file

Resources