1

I have working with cgi/python. I want to ask a question as below:

How to download a file with cgi/python?

I want when a page is loading then a file which specified in url will be downloaded.

I have googled in many days but can't until resolve this problem.

Anyone help me, Many Thanks.

Trung Nguyen
  • 617
  • 1
  • 8
  • 14
  • And this cgi-script will then serve the file that should be downloaded? – User Mar 06 '14 at 08:22
  • I think you need to serve with a `ContentType: application/octet-stream`. The the browser can not show the content then. Tell me if it works. – User Mar 06 '14 at 08:34
  • I typed `"python file download"` in google. Does the first link http://stackoverflow.com/questions/22676/how-do-i-download-a-file-over-http-using-python solve your problem ? – shuiyu Mar 06 '14 at 08:57
  • print "Content-Type: text/plain; charset=UTF-8" print "Content-Disposition: attachment; filename=%s.db;"%filename – Trung Nguyen Mar 06 '14 at 10:10
  • I have written this code to header then the result is fine. Many thanks for all. – Trung Nguyen Mar 06 '14 at 10:21
  • Do you mean: [Force browsers to download a file rather than open](http://stackoverflow.com/q/16124064/4279). See also [Utility of HTTP header “Content-Type: application/force-download” for mobile?](http://stackoverflow.com/q/10615797/4279) – jfs Mar 07 '14 at 02:29
  • @TrungNguyen Hi I am facing same problem, can u post ur updated code here ? https://stackoverflow.com/questions/44959862/how-to-download-a-file-in-python-jinja2-on-click-export-button – user3407267 Jul 17 '17 at 05:51

1 Answers1

1
import urllib
urllib.urlretrieve(url, filename)
fotocoder
  • 45
  • 3