How to download the Zip file from express js - node.js

I have three files, I have to zip and send, currently I am using archiver to zip them. I am trying to download the .zip file from express js. It prefectly works for text files, apparently it works for zip files even. but I can't see any data in the zip file after I download, it will be just like an empty text file
res.status(200).sendFile('./ABC.zip');
and it is a get request. tried setting
res.set('Content-Disposition', 'attachment; filename=file.zip');
res.set('Content-Type', 'application/zip');
But didn't help. do I need to set few more parameters for response object..?? please help

const downloadName = `${Date.now()}.zip`;
const data = zip.toBuffer();
res.set('Content-Type','application/octet-stream');
res.set('Content-Disposition',`attachment; filename=${downloadName}`);
res.set('Content-Length',data.length);
res.send(data);

Related

Is there an npm package for converting RTF strings to PDF file in node.js?

I'm looking for an RTF to PDF converter for Node.js. I'm not finding too many packages over at the npm site.
The application is as follows: I need to pull an RTF document (created in Word and saved as .rtf) from an AWS S3 bucket. Then I need to read the file as an RTF string. Next, I need to replace certain placeholder tokens in the RTF string with values (ex. in "{host-name} would like to invite you to {address} on {date}."--I need to replace {host-name}, {address}, and {date} with actual values). Then, with actual values plugged in, I need to convert the string to a PDF that I can attach to an email.
For this reason, I cannot use any of the several Word-to-PDF packages out there (because I need to intervene in the intermediate step of plugging the placeholders with values), so I'm looking for an RTF-to-PDF converted (where the RTF starts as a string).
Is there such a thing?
Thanks.
LibreOffice in headless mode should provide file conversion to PDF.
You can integrate it in node with LibreOffice-Convert.
However you'll need LibreOffice in your machine, as it's not installed with npm. See here for the location according to your system.
This is how it's used in the command line, so it supports RTF to PDF conversion:
soffice --headless --convert-to pdf file_name.rtf
(source)
In theory it should be implemented like this (adapted from npmjs):
const libre = require('libreoffice-convert');
const path = require('path');
const fs = require('fs');
const enterPath = path.join(__dirname, '/resources/example.rtf');
const outputPath = path.join(__dirname, '/resources/example.pdf');
// Read file
const file = fs.readFileSync(enterPath);
// Convert it to pdf format with undefined filter (see Libreoffice doc about filter)
libre.convert(file, '.pdf', undefined, (err, done) => {
if (err) {
console.log(`Error converting file: ${err}`);
}
// Here in done you have pdf file which you can save or transfer in another stream
fs.writeFileSync(outputPath, done);
});

How to receive .zip file in Flask from Angular UI?

I have an Angular application in which I used
<input type="file" (change)="fileChanged($event)">
to upload a .zip file.
I get the file like this:
fileChanged(e) {
this.file = e.target.files[0];
}
Now, I want to send this .zip file to my server (Python-Flask) and want to store it as a .zip file on my server-side.
I send the request to server like this:
const fd = new FormData();
fd.append('files', this.file, this.file.name);
fd.append('fileType', 'zip');
this.http.post('/my_app_route1/', this.file,
{headers:{'Content-Type': 'application/zip'}}).subscribe(f => {
....});
To receive and save this, I do the following on my python side.
#app.route('/my_app_route1/', methods=['POST'])
mydata = (request.get_data())
with open('hello.zip', 'wb') as f:
f.write(mydata)
Now, this data is binary data which I do not know how to write to a .zip file from Python.
When I try to print "mydata", I see glimpses of my original .zip file data which I had sent from front end mixed with other symbols.
Am I passing the .zip file right? If not how should I pass it?
Also, how can I save the .zip on my server-side using Python-Flask?
NOTE: The .zip file consists of multiple directories and sub-directories, and I want to replicate the same on my server-side.

Trying to convert xlsx to csv using a node stream

I have a lambda script that retrieves an email from s3, parses it with MailParser (streaming), transforms attachments to csv if necessary, and stores them in a different bucket. The script handles csv files (no conversion) and zip files, but I can't figure out how to convert xls to csv using streams.
Exceljs looks really good for this, but I can't get it to work for some reason (and I'm really new to streams so that's probably it).
var workbook = new Excel.Workbook();
var xstream = workbook.xlsx.createInputStream();
xstream.on('done', function(data) {
// convert to csv and s3.upload
});
attachment.stream
.pipe(xstream);
I'm getting an error
Error: Unexpected xml node in parseOpen
before the 'done' event so I'm not sure if I'm using createInputStream correctly.

Nodejs - Downloading and saving a PDF file yields empty, corrupt PDF

I'm trying to download and save a pdf file with nodejs and nightwatchjs, yet it does not work. PDF file remains empty (0kb) and I can't open it with Adobe Acrobat. Here's the code:
var url = "http://www.sait.ca/Documents/sample.pdf"
var file = fs.createWriteStream("form.pdf");
https.get(url, function(response){
response.pipe(file);
});
It looks like this method worked for others (see -> Download File, save it and read it again --> Error), but it does not for me. Am I missing something?
You need to call stream.end().
Store the file in your public folder and do an redirect like this
res.redirect(http://yourdomain.com/uploads/your_file.pdf);

How to add zip file without compression using node.js?

I am converting docx to epub using pandoc. After converting epub, i do changes with zip file and converting epub also. At that time,
I got the following issue after converting zip file to epub on mimetype file.
Mimetype contains wrong type (application/epub+zip expected).
I am searching a lot and get two logic
First one e.g., extra spaces, new line characters but it was going vain(there is no extra spaces, new line).
Add the mimetype file without compression in zip.
I am getting struck with second point. How to add mimetype file without compression in zip using node.js coding.
var archiver = require('archiver');
var archive = archiver('zip');
archive.file('d:\\xxxx'+'\\mimetype', { name:'mimetype'});
What is the problem in the above code and any attribute for zip?
Can any one assist me for adding file without compression in zip?
Thanks in advance.
add the zip option {store:true} for without compression
https://www.npmjs.com/package/archiver#store-boolean

Resources