File Corrupt problem. Downloading attachments in gmail and upload to our server using formdata and arraybuffer

490 views
Skip to first unread message

Julio Santolo

unread,
Dec 16, 2014, 8:02:15 AM12/16/14
to google-api-jav...@googlegroups.com
I am developing an application to capture the attachments of an email from gmail, allowing the user to select which to use, upload them to our server and link them to a particular project.
The code I present does not give any errors but the files uploaded to our server can not be opened, for example pdf viewer reports "corrupt file" the excel reports "some file formats lost this corrrupto" but repairs and can open.

  The application must:
   1) the application should compile a list of file name plus a checkbox for the user to select which files you want to keep and related to the project
   2) When the user presses "save" must download the checked attachments from gmail and upload it to our server
 
    Here is the step 2

                     $("input[name='chAdjuntos']:checked").each(function(index, element) {
               
var aValor = $(element).val().split(":");
var messageId = aValor[0];
var attachId = aValor[1];
var archivo = unescape(aValor[2]);
var mime = unescape(aValor[3]);
var userId = "me";
var request = gapi.client.gmail.users.messages.attachments.get({
'id': attachId,
'messageId': messageId,
'userId': userId
});
request.execute(function(attachment) {
var status = new createStatusbar();
status.setFileNameSize(archivo, attachment.size);
                                                // replace base64 url safe characters
var attData = attachment.data.replace("-","+").replace("_","/");
                                                // get arraybuffer
var attArray = base64DecToArr(attData).buffer;
                                                // construct blob
var blob = new Blob([attArray], { type:mime })
                                               
                                                // create formdata object and append blob and other data
var fd = new FormData();
fd.append("id", $(".resultado").attr("id"));
fd.append('blob', blob, archivo);

                                                // post file with jquery
sendFileToServer(fd, status);
});
});

   

function sendFileToServer(formData,status)
{
var uploadURL = "/includes/Upload.php"; //Upload URL
var extraData ={}; //Extra Data.
var jqXHR=$.ajax({
xhr: function() {
var xhrobj = $.ajaxSettings.xhr();
if (xhrobj.upload) {
xhrobj.upload.addEventListener('progress', function(event) {
var percent = 0;
var position = event.loaded || event.position;
var total = event.total;
if (event.lengthComputable) {
percent = Math.ceil(position / total * 100);
}
//Set progress
status.setProgress(percent);
}, false);
}
return xhrobj;
},
url: uploadURL,
type: "POST",
contentType:false,
processData: false,
cache: false,
data: formData,
success: function(data){
status.setProgress(100);
status.setListo();
nTotAdjuntos = nTotAdjuntos - 1  
if (nTotAdjuntos == 0) {
$("#btContinuar").show();
$("#espera").hide();
}
},
error: function() { 
alert("error al subir el archivo: "); 
$("#btContinuar").show();
$("#espera").hide(); 
}
}); 
status.setAbort(jqXHR);
}


What I am doing wrong ?
Thanks in advance.

Alex Wege

unread,
Sep 20, 2015, 6:59:23 PM9/20/15
to Google API JavaScript Client
I don't know if this will help at all or not, but I noticed your .replace("-","+").replace("_","/"); wasn't working. Using regex It should be: .replace(/-/g, '+').replace(/_/g, '/');  (from Stackoverflow).

I realize you posted this a long time ago, but just though id throw it up here anyways, it helped me fix my decoding problems.
Reply all
Reply to author
Forward
0 new messages