Good day. There is a code to receive base64 for transfer via json file (picture / archive).

function getBase64(file, callback) { var result = ''; var file_name = ''; var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function () { console.log("return file in base64: " + reader.result); var encoded_file = reader.result; console.log('return base64...'); callback(file.name,encoded_file); }; reader.onerror = function (error) { console.log('Error: ', error); return callback(file_name,''); }; } 

if this line decode in python

 attachment = MIMEBase('application', "octet-stream") attachment.set_payload(base64.b64decode(file['file64'])) email.encoders.encode_base64(attachment) attachment.add_header('Content-Disposition','attachment; filename="%s"' % file['filename']) msg.attach(attachment) 

That file is unreadable. Maybe I'm doing something wrong? Do you need to encode the file and decode it back? (btoa tried).

UPD1: Ok, let's reformat the question. There is a string obtained from the form via JSON type:

 data:image/jpeg;base64,/9j/4AAQSkZJR------TUT-ESCHO-BASE64-CODE------AAAAAAAAAAB/9k= 

How to decode it correctly in a file? or add letters to attach?

  • Are you trying to read the data URI? data = urllib.request.urlopen('data:...').read() - jfs
  • tried urlopen - alas, still unreadable output - Skif
  • urllib successfully works with data URI. For example, urlretrieve("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==", "image.png")
  • Yes, urlretrieve is it. I had to connect the truth tmpfile, because I didn’t want to work with (file, header) = retrieve (), But these are trifles. Thank. - Skif
  • urlretrieve is a wrapper for convenience around the urlopen. This means that urlopen works in your case. Simplified: urlretrieve = lambda url, path: copyfileobj(urlopen(url), open(path,'wb')) - jfs

1 answer 1

Thanks jfs for the hint. Urlretreive is required for decoding. Js code:

 function getBase64(file, callback) { var result = ''; var file_name = ''; var reader = new FileReader(); reader.readAsDataURL(file); reader.onload = function () { var encoded_file = reader.result; callback(file.name,encoded_file); }; reader.onerror = function (error) { console.log('Error: ', error); return callback(file_name,''); }; reader.readAsDataURL(file); } 

The actual python sticking code looks like this:

 attachment = MIMEBase('application', "octet-stream") f = tempfile.NamedTemporaryFile(mode='w+b', delete=False) filename = str(f.name) urllib.urlretrieve(file['file64'], filename) f.close FILE = open(filename) attachment.set_payload(FILE.read()) email.encoders.encode_base64(attachment) filename_header = file['filename'].encode('utf-8') attachment.add_header('Content-Disposition','attachment', filename=('utf-8', 'ru', filename_header)) msg.attach(attachment) FILE.close os.remove(FILE.name) 

You can do without temporary files, but I (filename, header) = urlretrieve () returned such a value that FILE perceived it as dict and refused to open even after casting to a string through str ()

  • You do not need a urlretrieve. Instead of FILE.read() , urlopen(file['file64']).read() : urlopen(file['file64']).read() . MIMEBase distracts from the essence of the issue (getting data from the data URI). Although it is not clear what prevents to send bytes as it is in javascript, without encoding them in the data URI. Then on the side of Python, FILE.read() is just file["file64"] . - jfs
  • urlopen (file ['file64']). read (). - I tried this option one of the first and it did not go - the file was created, but it was not displayed. data URI is used because the file is transmitted using JSON, and not get / post request - Skif
  • miracles do not happen. If urlretrieve() works for you, then urlopen().read() required to work. - jfs
  • So I did something wrong, but in this version I “started”, I am extremely happy with him, although the decision is clumsy. - Skif