I want to pass xlsx file as one of the request parameters (file) as below.
fields = {
"file": ('1.xlsx',open("file.xlsx", "rb"),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
"payload" : ""
}
But when I am passing file like above I am getting below exception or error in python
TypeError: a bytes-like object is required, not '_io.BufferedReader'
Can anyone help on this.
open() just opens the file for reading, you need to actually read the file bytes. Cannot tell fully from limited context, but if you don't need base64 then just drop that part out. The MIME type for binary data is "application/octet-stream"
Try this:
import base64
with open("file.xlsx", "rb") as xl_file:
fields = {
"file": ('1.xlsx',base64.encodestring(xl_file.read()),'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'),
"payload" : ""
}
# do something with fields
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.