Serving a file: Difference between revisions

From XPUB & Lens-Based wiki
(New page: Example of a Python script to serve a file as a "download" link. <source lang="python"> #!/usr/bin/env python import os filepath = "/path/to/the/file" filelen = os.path.getsize(filepath)...)
 
No edit summary
Line 5: Line 5:
import os
import os


filepath = "/path/to/the/file"
filepath = "/path/to/the/file.extension"
filelen = os.path.getsize(filepath)
filelen = os.path.getsize(filepath)



Revision as of 23:20, 27 May 2009

Example of a Python script to serve a file as a "download" link.

#!/usr/bin/env python
import os

filepath = "/path/to/the/file.extension"
filelen = os.path.getsize(filepath)

print "Content-type: application/download"
print "Content-length: %d" % filelen
print "Content-transfer-encoding: binary"
print "Content-disposition: attachment; filename=%s" % os.path.basename(filepath)
print

f = open(filepath, "rb")
bytesserved = 0
while True:
	data = f.read(1024)
	if not data:
		break
	bytesserved += len(data)
	sys.stdout.write(data)
f.close()