I'm creating a web app to read my e-mails using gmail api. All methods is working (users.messages.list, users.messages.get, etc...) and displaying in console.log and in my page HTML. One thing I have noticed that I have to use atob to decode the body.data and insert in my HTML. Now I have to download or read the attachment for exemple file.docx, and I'm using this exemple here after the callback I noticed that I have to decode too, but if I do, there is no link to download or read, only some code from microsoft word. If I copy this code and create a doc and paste it, says the file is corrupted.
My code:
function getAttachments(messageID, parts, callback) {
//console.log(parts);
var attachId = parts.body.attachmentId;
var request = gapi.client.gmail.users.messages.attachments.get({
'id': attachId,
'messageId': messageID,
'userId': 'me'
});
request.execute(function (attachment) {
callback(parts.filename, parts.mimeType, attachment);
});
}
if (att.length > 0) {
for (var i in att) {
getAttachments(response.id, att[i], function (filename, mimeType, attachment) {
console.clear();
console.log(filename, mimeType, attachment);
console.log(atob(attachment.data.replace(/-/g, '+').replace(/_/g, '/')));
inline.append('<a href="" style="display: block">' + filename + '</a>');
});
}
}
UPDATE
I found a solution here
window.location = 'data:'+mimeType+';base64,'+attachment.data.replace(/-/g, '+').replace(/_/g, '/');var link = 'data:' + mimeType + ';base64,' + attachment.data.replace(/-/g, '+').replace(/_/g, '/'); inline.append('<a href="' + link + '" style="display: block">' + filename + '</a>');