Django下载文件

 服务器端:

f = open(filename)
data = f.read()
f.close()

response = HttpResponse(data,mimetype='application/octet-stream') 
response['Content-Disposition'] = 'attachment; filename=%s' % filename
return response

用户端: 

#coding=utf-8

import urllib,time,datetime,urllib2,cookielib,json
import time,random,datetime

from poster.encode import multipart_encode
from poster.streaminghttp import register_openers

def getData():
        obj = {}
        obj['param'] = 'value'

        register_openers()
        datagen, headers = multipart_encode(obj)
        request = urllib2.Request('http://[url]', datagen, headers)
        result = urllib2.urlopen(request).read()
        
        fl = open('test.png','wb')#一定是wb,不然会保存不成功
        fl.write(result)
        fl.close()
      

if __name__ == '__main__':
    getData()
posted @ 2014-06-27 17:43  smallcoderhujin  阅读(228)  评论(0编辑  收藏  举报