GDrive API Upload files javascript

0

I followed all the steps and some other questions that I found in the stack, and using the following code I can upload a file to Google Drive:

var importFiles = $('#files')[0].files;

const boundary = '--pd_upload';
const delimiter = "\r\n"+boundary+"\r\n";
const close_delim = "\r\n"+boundary+"--";

var metadata = {
                'name': importFiles[0]["name"],
                'mimeType': importFiles[0]["type"],
                'parents':['parent-id']
            };

var multipartRequestBody =
                    delimiter+ 
                        'Content-Type: application/json; charset=UTF-8\r\n\r\n'+
                        JSON.stringify(metadata)+"\r\n"+
                    delimiter+"\r\n"+ 
                        'Content-Type: '+importFiles[0]["type"]+"\r\n\r\n"+
                        importFiles[0]+
                    close_delim;    

gapi.client.request({
                    'path': '/upload/drive/v3/files',
                    'method': 'POST',
                    'params': {
                            'uploadType': 'multipart'
                    },
                    'headers': {
                                'Authorization' : 'Bearer '+gapi.client.getToken()["access_token"],
                                'Content-Type': 'multipart/related; boundary="pd_upload"'
                    },
                    'body': multipartRequestBody

})

It seems like everything works fine, Google creates the file in the named folder with the name of the file, but the file can not be viewed as if it had not been sent. What am I doing wrong?

    
asked by anonymous 13.05.2018 / 22:31

1 answer

1

I had the same problem, however, I'm using RestSharp for HTTP requests.

My problem is that I was passing an image in the body in base64, but I should pass that image in binary.

You're probably passing the file in the wrong format. You can try using file.create to create the file in the correct format before submitting .

I do not know if it helps, but this is my code:

[HttpPost]
public string PostImage(){
    var restclient = new RestClient(BaseUrl);
    RestRequest request = new RestRequest("/upload/drive/v3/files?uploadType=media") {Method = Method.POST};

    string imageBase64 = "/9j/4AAQSkZJRgABAQAAAQABAAD/2wCEAAkGBwgHBgkIBwgKCgkLDRYPDQwMDRsUFRAWIB0iIiAdHx8kKDQsJCYxJx8fLT0tMTU3Ojo6Iys/RD84QzQ5OjcBCgoKDQwNGg8PGjclHyU3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3Nzc3N//AABEIAIUAyAMBEQACEQEDEQH/xA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b5IDIWe/FK3vGV55en6hflNVqqn+Ybvmng8/wBBUfg9R/Cy3Xr/AIkSxMmlNoaaZ/Ztv1WkPTdRJ8YM5ayqPk06PAa+ozm2YWHgDdy99PpMI97Xn7Hls17faCwdNhuCw0bfI3Pi46ldaMYxWIrCPA5NvLNQRADJWIHCNAODEA4NQC2CAVACAEAIAQAgBACAEAIAQAgBAIQCgI3QxnVqAidRQO1YEA32Cn+QICRlLE3RgQEzWNboEAtkAWQCoAQAgBACAEAIAQAgP//Z";

    byte[] bytes = Convert.FromBase64String(imageBase64);

   request.AddHeader("Content-Type", "image/jpeg");
   request.AddHeader("Content-Length", "5951");
   request.AddHeader("Authorization", "Bearer " + Authenticantion());
   request.AddParameter("application/json", bytes, ParameterType.RequestBody);

   var tResponse = restclient.Execute(request);
   var responseJson = tResponse.Content;

   return responseJson;
} 
    
03.06.2018 / 06:15