Bob Ezuba
Bob Ezuba

Reputation: 510

Python Tornado file download

I have multiple files in different directories that I need to users to be to access from a web page. They can view all text files within the browser but will download all *.zip files. I having difficulty with my code below in Python & Tornado for downloading the files

if directory.endswith('.zip'):
    print('Currently downloading:', url)
    self.set_header('Content-Type', 'application/octet-stream')
    self.set_header("Content-Description", "File Transfer")
    self.set_header('Content-Disposition', 'attachment; filename {}'.format(directory))

    with open(url, 'r') as f:
        try:
            while True:
                data = f.read(4096)
                if not data:
                    break
                self.write(data)
            self.finish()
        except Exception as exc:
            self.write(json_encode({'data': exc}))

I have looked at a lot of posts online and they suggest to do as I am doing but yet I am not getting the right results.

Please advise

Upvotes: 1

Views: 2370

Answers (1)

Bob Ezuba
Bob Ezuba

Reputation: 510

I figured the reason it was not working, I was sending the request using Ajax JQuery, which was getting the response and didn't know what to do with, so instead I just submitted the form and the file downloaded properly.

Upvotes: 1

Related Questions