1

ユーザーが大きな画像をアップロードしようとした場合に対処するために、チャンク メソッドを使用して画像を保存しようとしています。

destination = open(incident.Image.path, 'wb+')
for chunk in request.FILES['image'].chunks():
    destination.write(chunk)
destination.close()

私の問題は、最初に Image フィールドに次のように何かを保存しないと、ファイルパスを取得できないことです。

fileName = str(int(time.time() * 1000))
imageName = fileName + '.jpg'
incident.Image.save(imageName, request.FILES['image'])

私の質問は、最初に画像を保存せずに、どうすればincident.Image.pathに相当するものを取得できますか? パスをハードコーディングしたくありません。行き詰まった理由は、ImageField 宣言の upload_to 部分を取得しないと完全なファイルパスを取得できないためです。

編集:

わかりました、少し先に進みましたが、また行き詰まりました:

imgBuffer = StringIO.StringIO()
for chunk in request.FILES['image'].chunks():
    imgBuffer.write(chunk)

# rotate it
rotate = request.POST['rotation']
im = Image.open(imgBuffer)
im = im.rotate(float(rotate))

incident.Image.save(imageName, ContentFile(imgBuffer))

IOError cannot identify image file that is throw at im = Image.open(imgBuffer) を取得しています。何かアイデアはありますか?

4

3 に答える 3

0

stringIOもご覧くださいこのサイトもご覧ください

于 2012-06-08T14:39:44.087 に答える