콘텐츠로 건너뛰기
메뉴
커뮤니티에 참여하려면 회원 가입을 하시기 바랍니다.
신고된 질문입니다
2 답글
11486 화면

Dear All,

There this a Binary field in my customize model.

file = fields.Binary(string='File', required=True)

and I need upload the data(image) in this field to other URL with requests package.

The code below is workable:

URL = ......

with open('z.jpg','wb+') as f:
    f.write(base64.b64decode(self.file))
    f.seek(0)
    r = requests.post(URL, files={'media':f})

But have to write this data(image) to disk first. Can I fix this code and upload it without writing step?

아바타
취소

Do you know of the python library named StringIO ( https://docs.python.org/2/library/stringio.html )? This will allow you to mimic file creation but instead of saving it to the disk, it will be stored in memory (RAM). This might not exactly be the solution to your problem but at least allows you to circumvent writing to disk.

작성자

I tried code below:

r = requests.post(url=url, files={'media': io.BytesIO(base64.b64decode(self.file))}

But it still not work.

작성자 베스트 답변


아바타
취소
관련 게시물 답글 화면 활동
0
1월 22
2756
1
5월 21
8920
0
7월 19
2435
2
7월 17
10878
1
3월 16
4487