I want get content from my uploaded file. Uploaded file is just text file and i want read this file line by line
req.file("model").upload(function (err, uploadedFiles){
if (err) return res.serverError(err);
return res.json({
message: uploadedFiles.length + ' file(s) uploaded successfully!',
files: uploadedFiles,
content: uploadedFiles[0] // I want to get my uploaded file content
// Stream or buffer
});
});
You can get the file descriptor from uploadedFiles[0].fd
Use it to read / stream the file.
fs.readFile(uploadedFiles[0].fd, 'utf8', function (err,data) {
return res.json(200, {message: 'Ok', data: data});
})
First install Local filesystem streaming binary adapter for Sails.js / Waterline
npm install sails-local-fs
in your code create fs instance
var fs = require('fs');
And now use readFile() method to get content of your file
fs.readFile(files[0].fd, 'utf8', function (err,data) {
return res.json(200, {message: 'Ok', data: data});
})
Related
Below I have selected 3 files. On submit the the files get uploaded successfully to the destination. But also I want to read all the 3 files attributes like filename, path etc. on server side means
in nodejs. How to do that?
I am trying to fnd it in req.body and req.query by printing it in console but i am getting all as one object : [object Object]
Below is my node side code:
router.post('/api/upload/:cid',function(req,res){
// console.log("REQ",req); //file is there in the body
console.log("yes it came under api");
console.log("req.body = "+ req.body);
console.log("req.query = "+ req.query);
console.log("req.data = "+ req.data);
upload2(req,res,function(err) {
if(err) {
return res.end("Error uploading file.");
}
else
{
console.log("File is uploaded");
res.redirect('/taskswriter');
}
});
})
Here is the AJAX code:
$.ajax({
type: "POST",
url: $(this).attr('action'),
data: formData,
processData: false,
contentType: false,
success: function(r){
console.log("result",r)
},
error: function (e) {
console.log("some error", e);
}
});
if you are using multer on server side, to get files data (filename, path etc.) you need to get it from:
// If you are uploading multiples files
console.log(request.files);
// If you are uploading one file
console.log(request.file);
So, probably you will be able to get it with:
router.post('/api/upload/:cid',function(req,res){
console.log("req.files", req.files);
I've been stuck on this problem for awhile now and can't seem to figure out why this file isn't being uploaded to firebase correctly. I'm using the code below in firebase functions to generate a document, then I convert that document to a stream, finally I create a write stream declaring the path that I want the file to be written to and pipe my document stream to the firebase storage WriteStream.
Example 1: PDF uploaded from file system through firebase console. (Link works and displays pdf)
Example 2: PDF generated in firebase functions and written to storage using code below
Considerations:
I know the PDF is valid because I can return it from the function and see it in my web browser and everything is as I would expect.
When trying to open the bad file it doesn't display anything and redirects me back to the overview screen.
const pdfGen = require("html-pdf");
pdfGen.create(html, { format: "Letter" }).toStream(function (err, stream) {
if (err) return res.status(500).send(err);
var file = storage.bucket()
.file(job.jobNum + "/quote-doc.pdf")
.createWriteStream({
resumable : false,
validation : false,
contentType: "auto",
metadata : {
'Cache-Control': 'public, max-age=31536000'}
});
stream.pipe(file)
.on('error', function(err) {
return res.status(500).send(err);
})
.on('finish', function(data) {
stream.close();
file.end();
res.end();
console.log('finished upload');
});
});
I am using multer to upload images to the server . And i am using sharp to resize the images and then send to the client. But what happens is once a user uploads the image, multer uploads it to the server and sharp resizes it even but if the same user uploads again multer uploads the new file but sharp doesnt resize the new uploaded image rather it sends the original resized image.
app.post('/upload',(req,res)=>{
upload(req,res,(err)=>{
if(err){
res.render('picupload',{
msg: err
});
}else{
if(req.file == undefined){
res.render('picupload',{
msg: 'Error: No File selected!'
});
} else{
console.log(req.file);
sharp(`./public/uploads/${req.file.filename}`)
.resize(40,40)
.toFile(`./public/uploads/resize/${req.file.filename}`,(err,info)=>{
if(err) throw err;
console.log(info);
});
res.render(pathjoin13,{
file: `uploads/${req.file.filename}` ,
file1: `uploads/resize/${req.file.filename}`
});
}
}
});
});
The res.render() call depends on the output of the asynchronous toFile() call so will need to move to its callback, something like:
.toFile(`./public/uploads/resize/${req.file.filename}`,(err,info)=>{
if (err) throw err;
console.log(info);
res.render(pathjoin13,{
file: `uploads/${req.file.filename}` ,
file1: `uploads/resize/${req.file.filename}`
});
});
Hi i'm trying to download a pdf with nodejs (UNIREST) but i'm a bit stocked.
var url_pdf = "http://myurl.com/ex.pdf"
unirest.get(url_pdf)
.as.binary(function (response) {
fs.writeFile("./pdf/test.pdf", response.body, 'binary', function (err){
if (err)
console.log(err);
});
})
When wrting the file, its miss formatted (just a blank pdf file) size is ok.
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...