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
Related
I have a dummy csv file uploaded in my nodejs application. All I want is to convert the CSV to JSON file.
But it gives syntax error while just requiring the csv file.
Below is the code in nodejs to parse file content
const csv = require('csv-parser')
const results = [];
let csvToJson = require('convert-csv-to-json');
let fileInputName = require('./fileUpload/testingCSV.csv');
let fileOutputName = 'myproduct.json';
const fs = require('fs');
fs.createReadStream(fileInputName)
.pipe(csv())
.on('data', (data) => results.push(data))
.on('end', () => {
console.log(results);
});
csvToJson.generateJsonFileFromCsv(fileInputName,fileOutputName);
Below is the screenshot of console error
Also, the actual csv file will contain a large number of data of products/accounts/userprofiles etc.
this is a dummy csv that I am trying to parse.
Please let me know what needs to be done .
I think your csv file is not correcly formated, what is the name of your nodejs file, and can you give us the error line 14 please ?
EDIT :
you are probably using an old version of Node.js. If this is the case, either const csv = require('csv-parser'); to const csv = require('csv-parser/lib/es5');`
Or
the csv file which is incorrect. Give the path directly on createreadstream
I just want to be able to upload and download binary files to Strato Hidrive using node.js with webdav.
I tested uploading a jpg-image with the following code:
const createClient = require("webdav");
const fs = require("fs");
let client = createClient(
"https://myusername.webdav.hidrive.strato.com",
"myusername",
"mypassword"
);
let data = fs.readFileSync("./localfolder/logo.jpg", {encoding: "binary"});
client.putFileContents("/myfolder/logo.jpg", data, { "format": "binary", });
However when I check the uploaded file by downloading it through their web-client, it can't be opened and it seems to be corrupted.
Is there solution to this? Either by changing the code or by suggesting a free webdav space (other than Strato Hidrive) where it might be working?
Thanks a lot!
I just want to store my json data in a file in a particular directory using JS. I can not see the created file using the following code.
var jsonse = JSON.stringify(submitted);
var blob = new Blob([jsonse], {type: "application/json"});
var file = new File([blob], "" + workerID + ".json")
JS Documentation Link would also suffice.
Assuming you're not using a web browser which cannot write to your file system for, hopefully obvious (another question), security reasons.
You can redirect output from your script to a file.
node yourfile.js > output_file.json
Or you can use the fs module.
Writing files in Node.js
// note jsonse is the json blob
var fs = require('fs');
fs.writeFile("/tmp/test", jsonse, function(err) {
if(err) {
return console.log(err);
}
console.log("The file was saved!");
});
I am using the node-png library to make the png and then save it in the local directory but when I go to open it back up, it says that it does not exist. I want to either read in the data and send it out or just have the response send an <img> field with the picture. Here's what I have so far:
//write out img data
png.pack().pipe(dst);
//link to new image made
var link = fs.createReadStream('out.png'); //says that this does not exist
//write out response with img then delete file
response.writeHead(200, {"Content-Type": "image/png"});
response.end(link, 'binary');
Couldnt you just skip writing to file altogether?
var output = png.pack();
res.setHeader('Content-Type', 'image/png');
output.pipe(res);
If you do need to write it to disk simultaneously, refer to this answer:
https://stackoverflow.com/a/19561718/944006
If you need to write to disk FIRST and THEN read from that, you would do:
var output = png.pack();
output.pipe(dst, { end: false });
output.on('end', function(){
//Read your file and set your response here.
}
I didn't test any of this but that's the way piping works in general.
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