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?
data = urllib.request.urlopen('data:...').read()- jfsurlretrieve("data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==", "image.png")urlretrieve = lambda url, path: copyfileobj(urlopen(url), open(path,'wb'))- jfs