2017-05-18 1 views
0

병을 통해 이미지를 업로드하려고하고 psycopg2와 함께 포스트 그레스 BYTEA 열에 삽입하려고합니다. 이 오류 :"BYTEA 열에 이미지를 삽입하려고 할 때"_io.BufferedRandom을 이진수로 이스케이프 할 수 없음 "

TypeError: can't escape _io.BufferedRandom to binary 

데이터를 삽입 할 라인에서 cursor.excute().

@route('/images', method='POST') 
def upload_image(): 
    upload = request.files.get('image') 
    img = Image.open(upload.file) # Pillow 
    binary = psycopg2.Binary(upload.file) 
    cursor = connection.cursor() 
    id = cursor.execute(
     ''' 
     INSERT INTO image (filename, data, width, height) 
     VALUES (%s, %s, %s, %s) 
     RETURNING id 
     ''', 
     (upload.filename, binary, img.width, img.height) 
    ) 
    return id 

내가 잘못 뭐하는 거지 :

여기 내 코드입니까?

답변

1

psycopg2.Binary이 문자열을 예상하고 있지만 파일과 유사한 객체를 수신하고 있는지 여부는 문제가 아닌지 궁금합니다. 이 줄을 따라 뭔가를 시도해 봤니?

binary = psycopg2.Binary(upload.file.read()) 

참고 : 이전 행에 Image.open 전화를 (내가 추측하고있어) upload.file에서 모든 바이트를 소비하기 때문에, 먼저 파일의 시작 부분으로 추구해야 할 수도 있습니다.

+0

죄송합니다. 되돌아 와서 이것을 받아들이는 걸 잊었습니다.'file.seek (0)'와 함께 잘 작동했습니다 :) –

관련 문제