Transferring binary file from web server to client

Posted by Yan Cheng CHEOK on Stack Overflow See other posts from Stack Overflow or by Yan Cheng CHEOK
Published on 2010-12-23T08:49:25Z Indexed on 2010/12/23 8:54 UTC
Read the original article Hit count: 203

Filed under:

Usually, when I want to transfer a web server text file to client, here is what I did

import cgi

print "Content-Type: text/plain"
print "Content-Disposition: attachment; filename=TEST.txt"
print

filename = "C:\\TEST.TXT"
f = open(filename, 'r')
for line in f:
    print line

Works very fine for ANSI file. However, say, I have a binary file a.exe (This file is in web server secret path, and user shall not have direct access to that directory path). I wish to use the similar method to transfer. How I can do so?

  • What content-type I should use?
  • Using print seems to have corrupted content received at client side. What is the correct method?

© Stack Overflow or respective owner

Related posts about python