首页 > 代码库 > Django下载文件

Django下载文件

 服务器端:

f = open(filename)data =http://www.mamicode.com/ f.read()f.close()response = HttpResponse(data,mimetype=application/octet-stream) response[Content-Disposition‘] = attachment; filename=%s‘ % filenamereturn response

用户端: 

#coding=utf-8import urllib,time,datetime,urllib2,cookielib,jsonimport time,random,datetimefrom poster.encode import multipart_encodefrom poster.streaminghttp import register_openersdef 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()