这是我上传,我在一个基于类视图定义图像的代码,损坏的图像上传的Django
def _handle_uploaded_file(self, request):
folder = settings.MEDIA_ROOT
uploaded_filename = request.FILES['img_fl'].name
BASE_PATH ='/home/admin1/Desktop/virtualenv-12.1.1/mapfied'
# create the folder if it doesn't exist.
try:
os.mkdir(os.path.join(BASE_PATH, folder))
except Exception as e:
pass
# save the uploaded file inside that folder.
full_filename = os.path.join(BASE_PATH, folder, uploaded_filename)
fd = open(full_filename, 'wb')
file_content = ContentFile(request.FILES['img_fl'].read())
try:
for chunk in file_content.chunks():
fout.write(chunk)
fout.close()
html = "<html><body>SAVED</body></html>"
print(html)
except Exception as e:
print(e)
图像文件保存得到纠正与名的位置,但它是corrupted.I我无法找到确切的原因,我在这里做错了什么?
的缩进fout.close()似乎是错误的。另外,fout在使用前没有定义。 – Cheng
@Cheng Yup你是对的,但这也不是纠正问题 –