-1

Here is an answer which gives some information on how to base64 encode a file. However, I also want to pass in the filetype and mimetype. for the information in the base64 encoded string.

So far I have for my base64 string:

x=base64.b64encode(open('/Users/user/Desktop/img.PNG').read())

What is the correct information to prepend, and how would I do this?

Community
  • 1
  • 1
David542
  • 104,438
  • 178
  • 489
  • 842
  • 3
    You need to be more specific. How do you expect us to know what the correct information you need is, let alone how to represent it. We have no idea what you are doing with the base64 string... – Tom Dalton Jan 31 '15 at 23:37
  • @TomDalton thanks for the feedback. The user is passing a base64 string to us and we're uploading that file for the user to be able to download from a url. Having the extension is useful for doing something like 'http://myurl.com/file.png' – David542 Jan 31 '15 at 23:50
  • Are you looking for [`uu`](https://docs.python.org/3/library/uu.html)? – 5gon12eder Jan 31 '15 at 23:50
  • @David so you're writing a client application run on the user's machine? Isn't the file extension already part of the filename? Or are you really wanting to try to detect the file's actual mimetype, e.g. based on header information in the file itself? What other information are you considering under your 'etc'? – Tom Dalton Jan 31 '15 at 23:53
  • @TomDalton ok I updated my question to remove the `etc` ambiguity. Thanks for your feedback on that. – David542 Feb 01 '15 at 00:06

2 Answers2

4

It seems like the following is how I would get the base64 file information to pass to the server:

file = '/Users/user/Desktop/img.PNG'
prepend_info = 'data:%s;base64' % mimetypes.guess_type(file)[0]
base_64_data = open(file).read().encode('base64')
image_data_base64 = '%s,%s' % (prepend_info, base_64_data)

This then gives me:

data:image/png;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wB...
David542
  • 104,438
  • 178
  • 489
  • 842
1

Perhaps something along these lines:

from __future__ import print_function
import base64
import binascii
import os

def base64_encode_file(filename):
    filetype = os.path.splitext(filename)[1][1:]  # remove leading '.' from ext
    with open(filename) as file:
        data = file.read()
        return base64.b64encode(','.join((filename, filetype, data))), data

filename = 'C:/Users/martin/Desktop/img.PNG'
#filename = '/Users/user/Desktop/img.PNG'

encoded, data = base64_encode_file(filename)
print('encoded: {} (hex file data: {})'.format(encoded, binascii.hexlify(data)))

decoded = base64.b64decode(encoded).split(',', 2)
print('decoded:', decoded[0], decoded[1], binascii.hexlify(decoded[2]))

Output:

encoded: QzovVXNlcnMvbWFydGluL0Rlc2t0b3AvaW1nLlBORyxQTkcsiVBORwo= 
          (hex file data: 89504e470a)
decoded: C:/Users/martin/Desktop/img.PNG PNG 89504e470a
martineau
  • 119,623
  • 25
  • 170
  • 301
  • I only made `base64_encode_file()` return the both the encoded information and the raw file data to make it easy to verify that things had been done properly after the decoding was done -- so the `, data` on the `return` statement could be left off because only the encoded string needs to returned to get the functionality you want. – martineau Feb 01 '15 at 00:41