首页 > 其他 > 详细

Django下载文件

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

 服务器端:

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下载文件

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!