0

I would like to ask you, how to do force download of file from browser in Python. I have lot of pdf files, that I force downloading with PHP, to prevent opening them in browser

<?php
  if (isset($_GET['download'])) {
    $link = $_GET['download'];
    header('Content-disposition: attachment; filename='.$link.'');
    header('Content-type: application/pdf');
    readfile($link);
  }
?>

I have tried like half of the internet, I used urllib, urllib2, headers,.. I believe its a simple thing that I missed, because I just learn python and I want to rewrite my site into Python to learn it. I am able to create copy of pdf files, rename them, whatever. But it always remains on the server. It doesnt start download.

Any ideas please?

Thank you, have a great day..

Peter
  • 321
  • 1
  • 4
  • 10

1 Answers1

1

Check this link - http://css-tricks.com/snippets/htaccess/force-files-to-download-not-open-in-browser/

Add Lines to your apache conf (or htaccess)

AddType application/octet-stream .pdf

Worked perfectly for me.

[EDIT]

Check the link - Serve up pdf as a download with Pyramid, ningx, X-Accel-Redirect Header

Point to be noted -

Content-Disposition: attachment

Try an tell me if this worked.

Further Links on the Topic -
Content disposition

How to download a file using python in a 'smarter' way?

Community
  • 1
  • 1
Joddy
  • 2,557
  • 1
  • 19
  • 26
  • Thank you for answer, but this wont solve my problem. The php script above works without any problem. I have problem with python script. It does everything except download. I tried to use same headers, open(), etc.. I just need the correct way how to do it in Python. – Peter Nov 06 '12 at 08:20
  • Check the Answer on this link - http://stackoverflow.com/questions/12967734/serve-up-pdf-as-a-download-with-pyramid-ningx-x-accel-redirect-header It says you should add one more header field - > Content-Disposition: attachment – Joddy Nov 06 '12 at 09:37
  • sorry, I didn't have time to test it yesterday. I just checked all answers, combine them and still the same. It just doesnt start downloading. I will keep it open until I find the problem, after I will close it. – Peter Nov 07 '12 at 07:04
  • can you paste your python code here ? i need to look at what you are doing. – Joddy Nov 07 '12 at 13:45
  • I am out of ideas.. I tried request.add_header, webbrowser class to open it, nothing works. But what is strange, that it wont even open page via webbrowser. Can be this somehow broken on the server side settings on my hosting? Like prevention of opening links, files with cgi? I dont know much about that so just asking. Thanks – Peter Nov 08 '12 at 09:34
  • let me just refer this question to someone with better understanding. if previously your python page was opening properly, then it might not be server side problem – Joddy Nov 08 '12 at 14:52