1

我已经阅读了很多答案并在 shell 中尝试了所有这些,但不想正确保存我的本地图像,他截断了文件。当图像大小为 400kb 时,他在媒体目录中创建大小为 10-30kb 的文件。我不知道为什么。例如,我有路径为 d:/1.png 的图像。我试过了

from django.core.files import File
fileObject=File(open("d:/1.png"))
object.image.save('1.png',fileObject,True)

fileObject.size 显示正确的图像大小,但 object.image.size 不正确和文件,他保存的内容不完整。我也试过

from django.core.files.temp import NamedTemporaryFile
temp = NamedTemporaryFile()#with delete=True TypeError: __init__() got an unexpected keywork argument 'delete'
temp.write(open('d:/1.png').read())
temp.flush()
f=File(temp)#f.size not correct
object.image.save('1.png',f,True)
and object.image.size and file not correct, file not full.

我尝试使用 StringIO,但这也不起作用。我不知道是什么尝试正确保存这些图像。请帮忙。

4

1 回答 1

1

您必须将模式更改为二进制

fileObject=File(open("d:/1.png", mode="rb"))

于 2012-10-12T21:08:01.787 回答