Error while zipping a folder using Easyzip - node.js

Using Easyzip I am trying to zip a folder along with its files. The below mentioned code is not working properly. I get an error 'error occurred while extracting files'. Is there any other method to zip a folder with all its files and subfolders?
Here is my back-end code :-
var zip2 = new EasyZip();
zip2.zipFolder('./downloads/'+application._id,function(){
zip2.writeToFile('./downloads/'+application._id+'.zip');
res.setHeader('Content-disposition', 'attachment; filename='+application._id+'.zip');
res.setHeader('Content-type', 'application/zip');
var fs = require('fs');
var filestream = fs.createReadStream('./downloads/'+application._id+'.zip');
filestream.pipe(res);
});
});
My angular.js code
$http.post('/download/archive/' + stateParams._id, {year: year}).success(function (data) {
var file = new Blob([data], {type: 'application/zip'});
console.log(file)
saveAs(file, 'application.zip');
});
Please help me to solve these. Thanks in advance.

Even I had the same problem and was able to solve it. For more details check: Zip generated by EasyZip is not working properly Hope that this may help you.

Related

Node Express Fast CSV download to client

I've set up a small node js BE app, built with express and fastCsv module on top of it. The desired outcome would be to be able to download a csv file to the client side, without storing it anywhere inside the server, since the data is generated depending on user criteria.
So far I've been able to get somewhere it it, Im using streams, since that csv file could be pretty large depending on the user selection. Im pretty sure something is missing inside the code bellow:
const fs = require('fs');
const fastCsv = require('fast-csv');
.....
(inside api request)
.....
router.get('/', async(req, res) => {
const gatheredData ...
const filename = 'sometest.csv'
res.writeHead(200, {
'Content-Type': 'text/csv',
'Content-Disposition': 'attachment; filename=' + filename
})
const csvDataStream = fastCsv.write(data, {headers: true}).pipe(res)
})
The above code 'works' in some way as it does deliver back the response, but not the actual file, but the contents of the csv file, which I can view in the preview tab as a response. To sum up, Im trying to stream in that data, into a csv and push it to download file to client, and not store it on the server. Any tips or pointers are very much appreciated.
Here's what worked for me after created a CSV file on the server using the fast-csv package. You need to specify the full, absolute directory path where the output CSV file was created:
const csv = require("fast-csv");
const csvDir = "abs/path/to/csv/dir";
const filename = "my-data.csv";
const csvOutput = `${csvDir}/${filename}`;
console.log(`csvOutput: ${csvOutput}`); // full path
/*
CREATE YOUR csvOutput FILE USING 'fast-csv' HERE
*/
res.type("text/csv");
res.header("Content-Disposition", `attachment; filename="${filename}"`);
res.header("Content-Type", "text/csv");
res.sendFile(filename, { root: csvDir });
You need to make sure to change the response content-type and headers to "text/csv", and try enclosing the filename=... part in double-quotes, like in the above example.

write html content into word and download in user machine using node.js

How to use html-docx-js in node.js .
I have refered the npm site (https://www.npmjs.com/package/html-docx-js) for this. Below is the code
var converted = htmlDocx.asBlob(content);
saveAs(converted, 'test.docx');
But here is the problem, the saveAs function is missing in the tutorial.
I have downloaded filesaver.js on the client side i Iave implemented this and it works fine. But I want complete code in node.js running which it will convert my html content and will download a word file in client machine.
Looking forward for some help.
Regards,
Bikram Nayak.
var HtmlDocx = require('html-docx-js');
var fs = require('fs');
var html = 'fasfasfasdfsfsdfsf';
var docx = HtmlDocx.asBlob(html);
fs.writeFile('helloworld3.docx',docx, function (err){
if (err) return console.log(err);
console.log('done');
});

Can't write/append to JSON file in Node Webkit

I want to have persistent memory (store the user's progress) in a .json file in %AppData%. I tried doing this according to this post, but it doesn't work. For testing purposes I'm only working with storing one object.
The code below doesn't work at all. If I use fs.open(filePath, "w", function(err, data) { ... instead of readFile(..., it does create a json file in %AppData%, but then it doesn't write anything to it, it's always 0 bytes.
var nw = require('nw.gui');
var fs = require('fs');
var path = require('path');
var file = "userdata.json";
var filePath = path.join(nw.App.dataPath, file);
console.log(filePath); // <- This shows correct path in Application Data.
fs.readFile(filePath ,function (err, data) {
var idVar = "1";
var json = JSON.parse(data);
json.push("id :" + idVar);
fs.writeFile(filePath, JSON.stringify(json));
});
If anyone has any idea where I'm messing this up, I'd be grateful..
EDIT:
Solved, thanks to kailniris.
I was simply trying to parse an empty file
There is no json in the file you try to read. Before parsing data check if the file is empty. If it is then create an empty json, push the new data into it then write it to the file else parse the json in the file.

How to create project in node.js using html5 and mysql?

I want to include my another js file and one html file(dashboard.html).How it will possible?
var file = require('display.js'),
var http = require('http'),
fs = require('fs');
fs.readFile('./dashboard.html',file, function (err, html) {
if (err) {
throw err;
}
http.createServer(function(request, response) {
response.writeHeader(200, {"Content-Type": "text/html"});
response.write(html);
response.end();
}).listen(9236);
});
Please suggest me proper way to do this thing?
In nodejs if you want to add your own libraries/js.
use this
var file = require('./display.js'), // or the path to file it will load file from path
instead of
var file = require('display.js'), // here it will search in node_modules filder
You should know how node look for modules. I'm adding what I know.
If you will require some ./somefolder/somefile.js it will search for the path ./somefolder/somefile.js and load that file.
If you will add 'somefile.js it will search for the librery somefile.js to node_modules folder in your root directory. If it won't get the file there it will go to global path of node_modules.
If it didn't get that file in global node_modules also, it throws an error
Cannot find module <file.js>
Anyone can improve my answer if anything I missed.

How to link files in NodeJS (+ let the user download the file when clicking it)?

I guess it is just a simple question but I can't solve it on my own.
(I'm using Express + NodeJS)
What I would like to have is a directory listing with the files contained in it. The files shall be linked so that a user could download them by just clicking the link (like the standard directory listing you get if you have e.g. a apache server without any index file).
To list the directory content I use
var fs = require('fs');
fs.readdir('./anydir', function(err, files){
files.forEach(function(file){
res.send(file);
});
});
(Notice: I did not include any error handling in this example as you can see)
Now I tried to just link the file by modifying the
res.send(file)
to
res.send('<a href=\"' + file + '\">' + file + '<br>');
but this just prints out the error message:
Cannot GET /anydir/File
... because I did not handle every file request in app.js.
How can I achieve my goal mentioned above?
Just use express.directory and express.static as middleware, possibly with a user-defined middleware to set Content-Disposition headers.
This worked for me:
var fs = require('fs');
fs.readdir('/var/', function(err, files){
files.forEach(function(file){
res.write('<a href=\"' + file + '\">' + file + '<br>');
});
});
You put the content with write() not send().
Try this and let me know. I can see the list of files displayed correctly.
Hope this helps you.

Resources