コンテンツへスキップ
メニュー
この質問にフラグが付けられました
2 返信
11501 ビュー

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
2765
1
5月 21
8940
0
7月 19
2448
2
7月 17
10901
1
3月 16
4499