码迷,mamicode.com
首页 > 其他好文 > 详细

Django下载文件

时间:2014-07-10 00:40:41      阅读:204      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   文件   os   

 服务器端:

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()

Django下载文件,布布扣,bubuko.com

Django下载文件

标签:style   blog   http   color   文件   os   

原文地址:http://www.cnblogs.com/smallcoderhujin/p/3812466.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!