I got a task to make a button on web page and by clicking on that button I have to read and write a file of local system. How it can be possible.?
Can it be done in node.js or can it be possible if i make any browser app.
Kindly help.
You can read or write files using Nodejs using fs module.
To write file in Node js:
var fs = require('fs');
fs.writeFile(filename, data, [encoding], [callback])
To Read file in Node js :
var fs = require('fs');
fs.readFile(file, [encoding], [callback]);
Your browser doesn't have permission to edit files, you can run a javascript in node.js to read and write files.
var fs = require('fs');
var file = fs.readFile('package.json','utf-8');
console.log(file);
Related
I am new to electron and I wanted to develop a file explorer (cross platform application for desktop) using electron.
I am unable to read root folder.
How can i read root folder in windows, Linux and mac ?
I tried the following code but it resulted in an error:
const fs = require('fs');
const fileUrl = new URL('file:///Desktop');
fs.readFileSync(fileUrl);
if you want to read all files in a directory you should use another function in fs called fs.readdir or fs.readdirSync
// Callback
fs.readdir('/home/guest/projects', (err, files) => {
console.log(files);
})
// Sync
fs.readdirSync('/home/guest/projects')
I am trying to make a local JavaScript program to read a file, process it's contents and format the text line by line, store it in an array and also display it on the console.
I'm using NodeJS.
I don't need any assistance in the later steps. The problem is that the code posted everywhere runs on a browser. I'm looking for something that runs locally by directly picking up the file from the local address on the disk and displays the contents on a windows command prompt like interface.
// Import
var fs = require('fs');
// File path
fileName = <File name with path>;
// Read Function
fs.readFile(fileName, 'utf8', function (err, data) {
console.log(data);
});
This reads the file asynchronously from a file on the disk.
I'm working on a project where I need to store the data in files next to the html-file but it needs to be runnable even without internet, and without any local web server.
Could somebody tell me how to create/write a txt file what I'll use as script in my html?
Build your app using Electron (or NW.js) and use Node's filesystem module to write your text file:
const { writeFile } = require('fs');
const data = 'Some text';
writeFile('data.txt', data, (error) => {
console.log(error || 'Done!');
});
Seriously I can't create a file on my computer even in the same directory where the html file is in without webserver?
i want to download file that i upload to the server with node js and framework sailsjs
$scope.download = function(l,n){
console.log(n);
var fs = require('fs');
var http = require('http');
var file = fs.createWriteStream(n);
var request = http.get(l, function(response) {
response.pipe(file);
});
}
and when i click to download this error message i get:
ReferenceError: require is not defined
in this 2 file :
var fs = require('fs');
var http = require('http');
and i install before fs and http in npm install
thanks for the help
require() method is a method to import other files into your code in node.js.
node.js is server side javascript platform, which means it only runs on the server side. you're trying to write server side code on the client.
I can see you are using angular. if you're trying to make http requests to your server take a look at this: https://docs.angularjs.org/api/ng/service/$http
I was wondering how to write files in Meteor to the server. I was looking at this NodeJS code, but it wasn't working when I tried it in the server javascript code.
var fs = require('fs');
fs.writeFile("/client/test", "Hey there!", function(err) {
if(err)
console.log(err);
else
console.log("The file was saved!");
});
It was saying that require wasn't defined. Anyways, does anybody know how to write files to the server in Meteor?
You need to use Npm.require instead of just require.
var fs = Npm.require('fs');
This will work for any module that is part of node or meteor (such as fs so its not a problem here). However, for other npm modules you would have to use Meteor NPM or write your own smart package