How to receive .zip file in Flask from Angular UI? - python-3.x

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.

Related

ibm-cos-sdk putObject does not create the correct Binary Object

Following the instructions from the documentation, I try to upload a file on a bucket.
If I upload a text file (i.e. a csv file), everything works as expected.
If I upload a binary string file (i.e. a pdf file) (read using readAsBinaryString), the file on cloud storage is wrong, only blank pages. The file size and the 256hash of the file changes.
I used all:
A buffer Buffer.from(binaryString)
A Uint8Array new TextEncoder().encode(binaryString)
A the binary string
on
putObject({
Bucket: 'test-b',
Key: key,
Body: binaryString
})
Instead of using the binary stream of the file for PDF try to upload using the multipart file upload option to upload it.

Create CSV or PDF files without saving it to disc and stream it to AWS S3 Bucket

I have to create 2 files 1. PDF and 2. CSV, Both should be generated on a successful result.
I can generate PDF from HTML template using html-pdf package, I can create file stream without actually saving the file to disk, then stream the file using streaming-s3 package.
My problem starts with CSV files.
Curerently I'm creating a CSV file on the disk,
let testCsvData = ["Line 1", "Line 2", "Line 3", ..., "Line n"];
const testCSV = path.join('./test.csv');
fs.writeFileSync(testCSV, testCsvData.join(os.EOL));
Then createReadStream and get the file stream to push.
let testStream = fs.createReadStream('./test.csv');
I don't want to save the CSV file to the disk and then get the file stream. Thats simply waste of space right!
Is there any way that I can directly get the file stream without saving it?

How to download the Zip file from express 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);

Flask: Get gzip filename sent from Postman

I am sending a gzip file from Postman to a Flask endpoint. I can take that binary file with request.data and read it, save it, upload it, etc.
My problem is that I can't take its name. How can I do that?
My gzip file is called "test_file.json.gz" and my file is called "test_file.json".
How can I take any of those names?
Edit:
I'm taking the stream data with io.BytesIO(), but this library doesn't contain a name attribute or something, although I can see the file name into the string if I just:
>>>print(request.data)
>>>b'\x1f\x8b\x08\x08\xca\xb1\xd3]\x00\x03test_file.json\x00\xab\xe6RPP\xcaN\xad4T\xb2RP*K\xcc)M5T\xe2\xaa\x05\x00\xc2\x8b\xb6;\x16\x00\x00\x00'
Further to the comment, I think the code which handles your upload is relevant here.
See this answer regarding request.data:
request.data Contains the incoming request data as string in case it came with a mimetype Flask does not handle.
The recommended way to handle file uploads in flask is to use:
file = request.files['file']
file is then of type: werkzeug.datastructures.FileStorage.
file.stream is the stream, which can be read with file.stream.read() or simply file.read()
file.filename is the filename as specified on the client.
file.save(path) a method which saves the file to disk. path should be a string like '/some/location/file.ext'
source

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);

Resources