如何检查django中的文件大小限制

时间:2016-08-09 04:26:05

标签: python django file-upload

我正在创建一个上传文件的方法但是我想检查文件大小,因为我只想允许5mb作为最大限制。

我想做这样的事情

def handle_uploaded_file(thisFile):
    if thisFile > 5mb:
       return "This file is more than 5mb"
    else:
       with open('some/file/' + str(thisFile), 'wb+') as destination:
           for chunk in thisFile.chunks():
               destination.write(chunk)
       return "File has successfully been uploaded"

2 个答案:

答案 0 :(得分:4)

irisctree<-ctree(Species ~ Sepal.Length + Sepal.Width + Petal.Length + Petal.Width)
plot(itisctree,type="simple")

信用转到django snippet

答案 1 :(得分:3)

使用._size文件属性

if thisFile._size > 5242880:
    return "This file is more than 5mb"

._size以字节表示。 5242880 - 5MB

def handle_uploaded_file(thisFile):
    if thisFile._size > 5242880:
       return "This file is more than 5mb"
    else:
       with open('some/file/' + str(thisFile), 'wb+') as destination:
           for chunk in thisFile.chunks():
               destination.write(chunk)
       return "File has successfully been uploaded"