`
fangzhouxing
  • 浏览: 211590 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

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

阅读更多

升级到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解决了大文件上传时的内存占用问题。

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics