Sails.JS file upload - can't get a file on req.file() - node.js

I've just begun using Sails.JS and I tried to make a simple file upload. In Sails.JS, I've done this:
uploadFile: (req, res) => {
req.file('avatar').upload({
dirname: '../../assets/uploads'
}, function (err, uploadedFiles){
if (err) return res.serverError(err);
return res.json({
message: uploadedFiles.length + ' file(s) uploaded successfully!',
files: uploadedFiles
});
});
}
For Postman, I've done this:
The problem is, after I upload a file via Postman, I don't get a file in req.file(). However, console.log(req) does show my uploaded file's binary (or something like that). I just can't figure out what happened.

Related

Formidable using "end" event with file upload

I am using Formidable with Express in nodeJS in an attempt to have a simple single file upload scheme. I have confirmed that a file is actually sent over from the client-side, but where it seems to run into troubles is on the server-side.
index.js
app.post('/', (req, res) => {
const form = formidale();
form.on('file', (filename, file) => {
fs.rename(file.path, `./data/nodes.csv`, err => {
if (err) {
console.log(`There was an error in downloading a CSV file: ${err}`);
return null;
}
else {
console.log("CSV file has been uploaded correctly.");
}
});
});
form.on('error', err => {
console.log(`There was an error in downloading a CSV file: ${err}`);
return null;
});
form.on('end', () => {
console.log(fs.readFileSync('./data/nodes.csv')); // test to see if file exists
const nodes = assignMetrics();
console.log(nodes);
return nodes;
});
form.parse(req);
});
}
The main trouble I seem to find is that the form.on('end', ...) event does not seem to wait till the file has finished uploading to fire. I have confirmed this by trying to read the file in the event, but by that point it doesn't exist? The documentation though appears to suggest it is only meant to fire "after all files have been flushed [from the APIs pipe it infers]".
There appears to be no other events available that might wait till the file has been uploaded to be called? I also don't want to start throwing in layers of promises and such unless it is the only option, as each new layer of promises I find is a chance for unintended effects to happen.

Upload and Retrieve PDF file to PostgreSQL using Node.js, Express and Knex

I am trying to upload and retrieve a PDF file to and from a PostgreSQL Db using Node.js, Express and Knex.
The DB column documentfile is file type Bytea.
The file seems to be uploading to the DB and is returned. The resulting PDF file (test3.pdf) does not want to open. I have been struggling with this for a long time now. Any help will be greatly appreciated!
Node.js code to upload the file :
app.get('/uploadfile', (req,res) =>{
fs.readFile('0203.pdf' function(err, imgData) {
console.log(imgData)
knex('documenttable').insert(
{documentname: '0203.pdf',
documentfile: [imgData]
})
.then( function (result) {
res.end(console.log("Document uploaded To DB"));
});
});
});
Node.js code to retrieve the file:
app.get('/dbfileback', function(req, res, next) {
knex('documenttable').where({
documentnumber: '1'
}).select('documentname', 'documentfile')
.then( function (result) {
fs.writeFile('test3.pdf', result[0].documentfile,'binary');
res.end(console.log("Done"));
});
});

node, sails how to upload same file into two paths

I have created a function to upload image to a custom directory https://sailsjs.com/documentation/concepts/file-uploads .
code:
req.file('avatar').upload({
dirname: require('path').resolve(sails.config.appPath, 'assets/images')
},function (err, uploadedFiles) {
if (err) return res.negotiate(err);
return res.json({
message: uploadedFiles.length + ' file(s) uploaded successfully!'
});
});
how can I upload same file into 2 different paths.
the code I have tried:
var imgPathArr;
var dePathWeb = 'd:/images/web';
var dePath = 'd:/images/mobile';
imgPathArr.push(dePathWeb);
imgPathArr.push(dePath);
req.file('file').upload({
dirname: require('path').resolve(imgPathArr[0])
},function (err, uploadedFiles) {
if (err) return res.send(500, err);
console.log("uploadedFiles web " + uploadedFiles[0].fd);
req.file('file').upload({
dirname: require('path').resolve(imgPathArr[1])
},function (err, files) {
if (err) return res.send(500, err);
console.log("files mobile " + files[0].fd);
});
res.send("ok");
});
});
but each and every time the images are saved into a same path.
console:
uploadedFiles web c:\xampp\htdocs\meServer\images\secondNavi\000a073e-cd6c-4758-ab44-54d32ddfc20a.png
.
files mobile c:\xampp\htdocs\meServer\images\secondNavi\000a073e-cd6c-4758-ab44-54d32ddfc20a.png
why it always take the same path. the image is always only upload into the web directory. I'm defining the paths separately using array index.
Just use fsfrom node DOCS to copy this file.
Your code is really messy. Try to modulate that with a Controller + Service helper. Like ImageController dealing with req and res and ImageService to deal with everything else...

Downloading file in node using GridFS in production

I have an express app, which works when I run it locally. The issue is when downloading a file which as saved in mongoDB using GridFS. When running it locally (I just do ./bin/www and go to localhost:3000), I can download the file. But when I run it remotely, I download an html file.
This is the route which handles the response:
router.get('/getfile',function(req,res) {
if (req.isAuthenticated())
{
var gfs = Grid(mongoose.connection, mongoose.mongo);
var id = req.query.id;
gfs.exist({_id: id}, function (err, found) {
if (err) return handleError(err);
if (!found)
res.send('Error on the database looking for the file.')
});
var readStream = gfs.createReadStream({
_id: id
}).pipe(res);
}
else
res.redirect('/login');
});
and that is called by this line in a jade file:
td #[a(href="getfile?id=#{log.videoId}" download="video") #[span(name='video').glyphicon.glyphicon-download]]
On the server, I'm doing:
/logApp$ export NODE_ENV=production
/logApp$ ./bin/www
the mongoDB deamon is running. In fact, I can query the database. And I'm not writing any file! I want to read it.
EDIT: I found the error message:
MongoError: file with id #### not opened for writing
You need to move the code that pipes the file to the response into the gfs.exist callback so that it runs after the exist check.
gfs.exist({ _id: id }, function(err, found) {
if (err) {
handleError(err);
return;
}
if (!found) {
res.send('Error on the database looking for the file.')
return;
}
// We only get here if the file actually exists, so pipe it to the response
gfs.createReadStream({ _id: id }).pipe(res);
});
Apparently you get that generic "not opened for writing" error if the file doesn't exist.

How to upload a file and then display its contents in Node.Js Express app?

I'm trying to find an answer to this question for a long time: I need to upload a text/html file through my Node.Js express app and to save its contents into a variable for further treatment.
I can make the multipart form, of course, and post the content, but what I can access so far is only req.files which contains information about the file, but not the actual contents.
How do I get the actual text/html contents from the file?
I don't want to save it to my server, just pass on the content to my app and get rid of the file, so I don't want to use a module like formidable.
Can anyone help please?
Thank you!
The file is saved by default in a temporary folder (likely /tmp). You need to open the file, read its contents, and then delete it.
You'll want to use this API: http://nodejs.org/api/fs.html
And you could do this:
fs = require('fs');
fs.readFile(req.files.path, function (err, data) {
if (err) throw err;
// data will contain your file contents
console.log(data)
// delete file
fs.unlink(req.files.path, function (err) {
if (err) throw err;
console.log('successfully deleted ' + req.files.path);
});
});
Use multer https://github.com/expressjs/multer with the option inMemory: true.
As a crude example you'll want to do something like this.
app.post('test', function(req, res, next) {
var result = '';
multer({
inMemory: true,
onFileUploadData: function(file, data) {
result += data;
},
onFileUploadComplete: function(file) {
console.log(result); // This is what you want
}
})(req, res, next);
});

Resources