六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 28|回复: 0

使用新的Django 1.0文件上传写法

[复制链接]

升级  62%

7

主题

7

主题

7

主题

童生

Rank: 1

积分
31
 楼主| 发表于 2013-1-28 19:44:50 | 显示全部楼层 |阅读模式
升级到Django 1.0后,原来的文件上传程序不能运行,报告了下列错误:
 
[2008-09-17 Wed 10:20:52]ERROR   "<type 'exceptions.TypeError'>
'InMemoryUploadedFile' object is unsubscriptable
[('/home/dev2/deploy/divo3/apps/xf/views/check_report_content.py', 297, 'add_system1_pic', "fd.write(file['content'])")]"
 
原来的上传程序如下:
 
        file = request.FILES['file']
        file_name = get_unique_file_name()  #生成唯一的文件名
       
        path1 = os.path.join(settings.DIVO_TEMP_ROOT, 'xf')
   
        fd = open('%s/%s' % (path1, file_name), 'wb') 
        fd.write(file['content']) 
        fd.close()
 
现在修改为:
 
        file = request.FILES['file']
        file_name = get_unique_file_name()
       
        path1 = os.path.join(settings.DIVO_TEMP_ROOT, 'xf')
        destination = open('%s/%s' % (path1, file_name), 'wb+')
        for chunk in file.chunks():
            destination.write(chunk)
        destination.close()
 
从上面的新程序中可以看出,Django解决了大文件上传时的内存占用问题。
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表