How can I append data to a file using node.js
I already have a file named myfile.json with data. I want to check if the file name exists and then append some data to that file.
I'm using following code
var writeTempFile = function (reportPath, data, callback) {
fs.writeFile(reportPath, data, function (err) {
//if (err) //say(err);
callback(err);
});
}
writeTempFile(reportDir + '_' + query.jobid + ".json", data, function (err) {
context.sendResponse(data, 200, {
'Content-Type': 'text/html'
});
You can use jsonfile
var jf = require('jsonfile');
var yourdata;
var file = '/tmp/data.json';
jf.readFile(file, function(err, obj) {
if(!err) {
var finalData = merge(obj, yourdata);
jf.writeFile(file, finalData, function(err) {
console.log(err);
});
}
});
You need to implement your merging logic in merge(object1, object2)
https://npmjs.org/package/jsonfile
Check out the following code.
function addToFile(reportPath, data, callback){
fs.appendFile(reportPath, data, function (err) {
callback(err);
});
}
Node offers fs module to work with file system.
To use this module do
var fs = require('fs')
To append some data to file you can do:
fs.appendFile('message.txt', 'data to append', function (err) {
if (err) throw err;
console.log('The "data to append" was appended to file!');
});
Node offers you both synchronous and asynchronous method to append data to file, For more information please refer to this documentation
Related
How to read the data of a file(say, read.txt) without getting the buffered Data?
I mean I want to get the content of the file directly without getting the buffer data?
and hoe to delete the folder in Node.js?
// Try this //
fs = require('fs')
fs.readFile('/read.txt', 'utf8', function (err,data) {
if (err) {
return console.log(err);
}
console.log(data);
});
try {
fs.rmdirSync(dir, { recursive: true });
console.log(`${dir} is deleted!`);
}catch (err) {
console.error(`Error while deleting ${dir}.`);
}
i'm developing a web application using html, javascript and nodejs.
I have to store on the server some pdfs and so the way that i found to send pdf from client to server is to use FormData object.
Client side my code is:
var files = new FormData();
var count = 0;
$('#tableSlideId tr').each(function() {
var inputForm = $(this).find("th:first").children();
file = inputForm[0].files[0];
files.append((count++).toString(),file);
});
$.ajax({
type: "POST",
url: "/sendFiles",
data: files,
contentType: false,
processData: false,
}).done(function(err){
var text ="";
if(err) {
text = "Upload FAILED! Retry ...";
} else {
text = "Upload SUCCES!";
}
alert(text);
});
Now server side i need to access to each file stored in the FormData object in order to store them in a server's folder. So i try with the following code:
app.post('/sendFiles', function(req,res) {
new formidable.IncomingForm().parse(req, (err, fields, files) => {
if (err) {
console.error('Error', err)
throw err
}
// console.log('Files', files);
daoQuery.insertPdf(files);
})
res.end();
});
where daoQuery.insertPdf() function is defined as:
insertPdf: function(files){
//var form = new FormData(files);
//console.log(files);
for(var key in files){
//console.log("--->", files[key])
fs.writeFile(".\\dataset\\newFile.pdf", files[key].path, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
}
}
In this way I obtain a pdf file in the folder but it has 1KB size(that is not the original file's size) and if i try to open it the system says that the file is damaged.
I'm quite sure that the FormData object is recieved correctly from the server, infact in "insertPdf" function the print console.log('Files', files) gives me all the fields of the file ( like name, size, type = application/pdf, path and so on).
I solved modifying the insertPdf function as follow:
insertPdf: function(files){
//var form = new FormData(files);
//console.log(files);
for(var key in files){
//console.log("--->", files[key])
fs.readFile(files[key].path, function(err1,data){
if(err1) throw err;
fs.writeFile(".\\dataset\\newFile.pdf", data, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
});
i don't get how to properly use jsZip..
i'm trying to get a file from a folder in localhost then zip it and at last save the zipped file in the same folder...
but really i don't have good comprension on how to use it
here what i have done :
JSZipUtils.getBinaryContent("http://localhost:8100/pdf/test.pdf", function (err, data) {
if(err) {
alert('err');
throw err;
}
else{
zip.file("test.pdf",data,{binary:true})
.then(function (blob) {
saveAs(blob, "pdf.zip");
});
}
});
can anibody help me?
The following bit of code is what ended up working for me.
NB, this is for jszip utils version 3. It needs filesaver.js to make the saveAs call work (https://github.com/eligrey/FileSaver.js).
Here's my code:
function create_zip(){
// get checked file urls
var urls = Array();
$("input:checked").each(function () {
urls.push($(this).siblings('a').attr('href'));
})
console.log(urls);
var zip = new JSZip();
var count = 0;
var zipFilename = "archive.zip";
urls.forEach(function(url){
var filename = url.substr(url.lastIndexOf("/")+1);
console.log(filename);
// loading a file and add it in a zip file
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
throw err; // or handle the error
}
zip.file(filename, data, {binary:true});
count++;
if (count == urls.length) {
zip.generateAsync({type:'blob'}).then(function(content) {
saveAs(content, zipFilename);
});
}
});
});
};
This is adapted from the info here: https://gist.github.com/noelvo/4502eea719f83270c8e9
Hope it helps!
var zip = new JSZip();
JSZipUtils.getBinaryContent(url, function (err, data) {
if(err) {
alert('err');
throw err;
}
else{
zip.file("test.csv",data,{binary:true});
zip.generateAsync({ type: "Blob" }).then(function (content) {
fsaver.saveAs(content, "example.zip");
});
}
});
try this for angular. this one is for csv file. give url as the path of the csv file.
I am reading below data from file now i want to create json object from it,how can i create that using nodejs fs module ?
app.js
var path = "./ditLogs/" + file;
fs.readFile(path, function(err, data) {
console.log('reading file data', data.toString());
});
Here is data in file that i need to create json for
file.txt
{"event":"test data"}
{"event":"test data"}
{"event":"test data"}
You can use this sample function:
function(strFromFile) {
try {
return JSON.parse(strFromFile);
} catch(e) {
return {};
}
};
FS Module
While developing in NodeJS the need to access a computer's file system is sometimes necessary. FS Module is a utility that assists with this process.
FS includes some functions that allow for different activities to be done on the file system via a wrapper around the API.
This should be included/required/imported into any JS that needs to interact with the file system API.
var fs = require("fs");
These are the different methods you can use on this API which are all asynchronous:
fs.readFile {fs.readFile('input.txt', function(err, data)
// Asynchronous read
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
fs.writeFile
fs.writeFile('input.txt', 'Simply Easy Learning!', function(err) {
if (err) {
return console.error(err);
}
console.log("Data written successfully!");
console.log("Let's read newly written data");
fs.readFile('input.txt', function (err, data) {
if (err) {
return console.error(err);
}
console.log("Asynchronous read: " + data.toString());
});
});
open
// Asynchronous - Opening File
console.log("Going to open file!");
fs.open('input.txt', 'r+', function(err, fd) {
if (err) {
return console.error(err);
}
console.log("File opened successfully!");
});
fs.stat (provides information about the file) e.g.
fs.stat('input.txt', function (err, stats) {
if (err) {
return console.error(err);
}
console.log(stats);
console.log("Got file info successfully!");
// Check file type
console.log("isFile ? " + stats.isFile());
console.log("isDirectory ? " + stats.isDirectory());
});
fs.read (similar to readFile and should not be the first choice for reading a file)
fs.close
// Close the opened file.
fs.close(fd, function(err){
if (err){
console.log(err);
}
console.log("File closed successfully.");
});
ftruncate (truncate an opened file)
unlink (delete an opened file)
fs.unlink('input.txt', function(err) {
if (err) {
return console.error(err);
}
console.log("File deleted successfully!");
});
fs.mkdir (make new directory)
fs.mkdir('/tmp/test',function(err){
if (err) {
return console.error(err);
}
console.log("Directory created successfully!");
});
fs.readdir (reads a directory)
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
fs.rmdir (remove directory)
fs.rmdir("/tmp/test",function(err){
if (err) {
return console.error(err);
}
console.log("Going to read directory /tmp");
fs.readdir("/tmp/",function(err, files){
if (err) {
return console.error(err);
}
files.forEach( function (file){
console.log( file );
});
});
});
Synchronous functions:
readFileSync
// Synchronous read
var data = fs.readFileSync('input.txt');
console.log("Synchronous read: " + data.toString());
writeFileSync
// Synchronous write
var data = fs.writeFileSync('input.txt', 'asdasdasd');
Simply read using line-by-line package, less headache, more control on reading process (it can pause, resume reading, close file descriptor ondemand, skip N lines) with less code.
1) install:
npm i --save line-by-line
npm i --save lodash
2) implement
var lineByLine = require('line-by-line'),
_ = require('lodash'),
path = require('path');
var lines = [];
var filePath = path.join(__dirname, "ditLogs", file);
var fileReader = new lineByLine(filePath);
fileReader.on('line', function(line) {
line = JSON.parse(line);
if(_.isPlainObject(line) && !_.isEmpty(line)) {
lines.push(line);
}
// optional
doSomethingWithLine(line);
});
function doSomethingWithLine(line) {
// for example You can save to db or send to somewhere using request libary or just show in console
}
fileReader.on('error', function(error) {
console.error(error);
process.exit(-1);
});
fileReader.on('end', function() {
doSomethingAfterParsingAllLines(lines);
});
function doSomethingAfterParsingAllLines(records) {
// do something with data
}
'use strict';
const fs = require('fs');
let rawdata = fs.readFileSync('student.json');
let student = JSON.parse(rawdata);
console.log(student);
Weird error, I use blueimp jquery file upload, to upload files from client-side to Node.js then Amazon S3 storage.
the code works perfect, the files does get to Amazon s3, but on client-side on the plugin I get this error: Error SyntaxError: Unexpected token S.
The Node.js code I'm using is the following:
app.post('/upload', function(req, res) {
fs.readFile(req.files.files[0].path, function (err, data) {
var imageName = req.files.files[0].name
/// If there's an error
if(!imageName){
console.log("There was an error")
res.redirect("/");
res.end();
} else {
var img_path = req.files.files[0].path;
var file_read = fs.readFile(img_path, function(err, data){
var BucketName = 'webwedding/'+SSName;
s3.createBucket({Bucket: BucketName}, function() {
var params = {Bucket: BucketName,ACL: "public-read" , Key: imageName, Body: data};
s3.putObject(params, function(err, data) {
res.writeHead(200, {'Content-Type': 'text/plain'});
if (err) {
console.log(err)
res.end(err);
}
else {
console.log('Uploaded ' + imageName +' To '+SSName);
res.end('Successfully uploaded data to webwedding\n');
}
});
});
});
}
});
});
I now came to understand that I need to JSON.stringly the result, but only cant manage to create the correct res.end result! trying to follow Blueimp support.
*Edit:*trying to add template to check results:
var files = [
{
"name": "picture1.jpg",
"size": 902604,
"url": "http:\/\/example.org\/files\/picture1.jpg",
"thumbnailUrl": "http:\/\/example.org\/files\/thumbnail\/picture1.jpg",
"deleteUrl": "http:\/\/example.org\/files\/picture1.jpg",
"deleteType": "DELETE"
}]
res.end(JSON.stringify(files));
gives me that error on client side:
Error Empty file upload result
The error comes from fileupload.ui code, hope it's not too long, but I think I must show it so maybe someone can figure it out:
if (data.context) {
data.context.each(function (index) {
var file = files[index] ||
{error: 'Empty file upload result'};
deferred = that._addFinishedDeferreds();
that._transition($(this)).done(
function () {
var node = $(this);
template = that._renderDownload([file])
.replaceAll(node);
that._forceReflow(template);
that._transition(template).done(
function () {
data.context = $(this);
that._trigger('completed', e, data);
that._trigger('finished', e, data);
deferred.resolve();
}
);
}
);
});