Transfer Text File in Web Server to Client
<p>I try to use the following script to transfer a text file located in web server, to client (The text file is in UTF-16).</p>

<pre><code>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
</code></pre>

<p>However, when I open up the downloaded file, the file is all having weird characters. I try to use <code>rb</code> flag, it doesn't either.</p>

<p>Is there anything I had missed out? What I wish is, the file (TEST.TXT) downloaded by the client by making query to the above script, will be exactly same as the one in server.</p>

<p>I also try to specific the encoding explicitly.</p>

<pre><code>import cgi

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

filename = "C:\\TEST.TXT"
f = open(filename, 'r')
for line in f:
    print line.encode('utf-16')
</code></pre>

<p>That doesn't work either.</p>

<hr>

<p><strong>Original File on Server</strong>
<img src="http://i.stack.imgur.com/S6SjX.png" alt="alt text"></p>

<hr>

<p><strong>Downloaded File</strong>
<img src="http://i.stack.imgur.com/l39Lc.png" alt="alt text"></p>

<hr>

<p>I will have the <a href="https://sites.google.com/site/yanchengcheok/Home/TEST.TXT?attredirects=0&amp;d=1" rel="nofollow">original text file</a> being posted here in case you are interested to experiment it out.</p>
