我正在創建一個上傳文件的方法,但是我想檢查文件大小,因爲我只是想允許5mb作爲最大限制。如何檢查django中的文件大小限制
我想讓這樣的事情
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"
隨着你的Django的檢查,我還建議加入一些服務器級別配置,以限制上傳文件大小(例如塞汀'client_max_body_size'在nginx的)。 –