I am trying to upload a file to an another server. I am trying to do it like that:
package = Package.objects.get(id=package_id)
with open(package.logo_image.image, 'rb') as image_handle:
image_data = bytes(image_handle.read())
image = {
'type': 'IMAGE',
'data': image_data,
'xsi_type': 'Image'
}
logo_image = media_service.upload(image)[0]
Field in a model:
image = models.ImageField(upload_to=generate_file_path, null=True, blank=True)
After trying to run this code I get:
with open(package.logo_image.image, 'rb') as image_handle: TypeError: expected str, bytes or os.PathLike object, not ImageFieldFile
Converting to str() does not help.
Is is possoble to open this file i binary mode? Thank you!
You don't need to open the file at all. ImageField (and FileField) give you direct access to the underlying file object.
package = Package.objects.get(id=package_id)
image_data = bytes(package.logo_image.image.read())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With