1// fs_write.js
2
3const fs = require('fs');
4
5// specify the path to the file, and create a buffer with characters we want to write
6let path = 'ghetto_gospel.txt';
7let buffer = new Buffer('Those who wish to follow me\nI welcome with my hands\nAnd the red sun sinks at last');
8
9// open the file in writing mode, adding a callback function where we do the actual writing
10fs.open(path, 'w', function(err, fd) {
11 if (err) {
12 throw 'could not open file: ' + err;
13 }
14
15 // write the contents of the buffer, from position 0 to the end, to the file descriptor returned in opening our file
16 fs.write(fd, buffer, 0, buffer.length, null, function(err) {
17 if (err) throw 'error writing file: ' + err;
18 fs.close(fd, function() {
19 console.log('wrote the file successfully');
20 });
21 });
22});
1function convertToJSON() {
2 var firstname = document.getElementById('firstname').value;
3 var lastname = document.getElementById('lastname').value;
4 var email = document.getElementById('email').value;
5
6 var jsonObject = {
7 "FirstName": firstname,
8 "LastName": lastname,
9 "email": email
10 }
11
12 document.getElementById('output').value = JSON.stringify(jsonObject)
13}
14
15function saveToFile() {
16 convertToJSON();
17 var jsonObjectAsString = document.getElementById('output').value;
18
19 var blob = new Blob([jsonObjectAsString], {
20 //type: 'application/json'
21 type: 'octet/stream'
22 });
23 console.log(blob);
24
25 var anchor = document.createElement('a')
26 anchor.download = "user.json";
27 anchor.href = window.URL.createObjectURL(blob);
28 anchor.innerHTML = "download"
29 anchor.click();
30
31 console.log(anchor);
32
33 document.getElementById('output').append(anchor)
34
35
36}