Skip to Content
เมนู
คุณต้องลงทะเบียนเพื่อโต้ตอบกับคอมมูนิตี้
คำถามนี้ถูกตั้งค่าสถานะ
2 ตอบกลับ
11394 มุมมอง

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.

ผู้เขียน คำตอบที่ดีที่สุด


อวตาร
ละทิ้ง
Related Posts ตอบกลับ มุมมอง กิจกรรม
0
ม.ค. 22
2681
1
พ.ค. 21
8826
0
ก.ค. 19
2376
2
ก.ค. 17
10808
1
มี.ค. 16
4421