When I use download() in CasperJS, I get a file saved in the system, but the file doesn't contain the actual source code of the webpage. It just contains a link to the remote page. How can I dump the source code of webpage into a local file using CasperJs? getHTML() is also only echoing the contents onto the terminal. How to save the contents to a file?
First import file system library
var fs = require('fs');
Extract html
var html = this.getHTML();
// or
var html = this.getPageContent();
Copy into a file
var f = fs.open('/path/to/your/file', 'w');
f.write(html);
f.close();
do just: fs.write('path/to/file', 'your string', 'w');
in this case you don't need open and close a file
Related
I am looking for a way to display the contents of a directory on an HTML page. I have the following snippet of code:
const fs = require("fs");
let directory_name = "/example";
let filenames = fs.readdirSync(directory_name);
console.log("\nFilenames in directory:");
filenames.forEach((file) => {
console.log("File:", file);
});
This will display the contents of the file in the terminal(console) but I am looking for a way to change the "console.log("File:", file)" line into a statement to send the elements to my HTML page.
Any help would be appreciated.
Using Linux btw
Use the express.js res.send function.
I want to change my JSON file or add an element to my SON file, but real file. I tried this code, but it doesn't work on the real file. Only the time the tab is open on the web has changed. How to handle it in real file? Not user file, it is server file, but I tried my local.
let xmlreq = new XMLHttpRequest()
xmlreq.open("GET","users.json",true)
function test(){
const obj = JSON.parse(xmlreq.responseText);
console.log(obj);
obj.user1.name="john";
console.log('obj.user1.name: ', obj.user1.name);
obj.user2.push("item");
console.log('obj.user2.: ', obj.user2);
}
xmlreq.send()
another
let xmlreq = new XMLHttpRequest()
function test(){
// let parsereq= JSON.parse(xmlreq.responseText);
const obj = JSON.parse(xmlreq.responseText);
console.log(obj);
obj.user1.name="john";
console.log('obj.user1.name: ', obj.user1.name);
obj.user2.push("item");
console.log('obj.user2.: ', obj.user2);
}
xmlreq.open("GET","users.json",true)
xmlreq.send()
First you have to use the File API to load the file.
https://developer.mozilla.org/en-US/docs/Web/API/File
Then you have to parse the JSON data.
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/parse
Then you can do your modifications.
But you can not modify files on your local disc directly. Instead you have to download the file in order to overwrite the original file.
To do so you have to create a data URL from your JSON data.
https://developer.mozilla.org/en-US/docs/Web/HTTP/Basics_of_HTTP/Data_URIs
And finally you can create a link to download the new JSON.
https://stackoverflow.com/a/15832662/402322
my need is on a click button, I need to get a pdf file from a directory and download as same pdf file in a browser.
I have a pdf file with 4-5 pages.
below is my code to read the pdf file I used fs.createReadStream
var fs = require('fs');
var data = '';
var readStream = fs.createReadStream('D:/Passbook.pdf', 'utf8');
readStream.on('data', function(chunk) {
data += chunk;
}).on('end', function() {
console.log(data);
});
here as a first step I would like to see the data for what I have fetched, but I the console log shows me something gibberish texts like below
ocProps/app.xmlPK-!t?9z�(CpcustomXml/_rels/item1.xml.relsPKIr
+100 lines
my plan is to if I can see the text then I would do fs.writeFile in pdf format, but I'm not sure whether it is possible to write a pdf file and it should be fetched and downloadable for user.
note - I'm using node version of 6.10.0
How to open a .js file from c# winform? I tried this
Process.Start(#"C:\...\software.exe", #"C:\...\mergescripts.js");
but it's not working. Manually, I can load the mergescripts.js file into my on software.exe using open file. But how to do it dynamically using button event from c#?
JintEngine can be used to execute JavaScript:
using (FileStream fs = new FileStream("file.js", FileMode.Open))
{
JintEngine js = JintEngine.Load(fs);
object result = js.Run("return status;");
Console.WriteLine(result);
}
please try with below syntax
ProcessStartInfo ps = new ProcessStartInfo();
ps.FileName = #"C:\Program Files (x86)\Notepad++\notepad++.exe";
ps.Arguments = #"C:\Users\Desktop\aaa.txt";
Process.Start(ps);
I have a jsp file in which i am writing a function in javascript that takes as argument, a tar file name and returns a list of the file names of the files that the tar file contains.. How can it be done?
Have you tried using UnTar.js ? Its open source js library for that
edit :
providing usage example for clarity
function updateProgressBar(e) { ... update UI element ... }
function displayZipContents(e) { ... display contents of the file ... }
var unzipper = new bitjs.archive.Unzipper(zipFileArrayBuffer);
unzipper.addEventListener("progress", updateProgressBar);
unzipper.addEventListener("finish", displayZipContents);
unzipper.start();
some more info here