0

I can upload the pdf file, but it will upload as blank/empty file. I don't know what am i missing from here.

Backend i receive the file, i also tried without converting to Base64 and still the same thing.

     using (var sr = new StreamReader(file.OpenReadStream(), System.Text.Encoding.UTF8))
            {
                _fContent = await sr.ReadToEndAsync();
                var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(_fContent);
                _fContent = System.Convert.ToBase64String(plainTextBytes);
            }          

Frontend i create the request.

      endpoint = 'https://www.googleapis.com/upload/drive/v3/files?uploadType=multipart&fields=id';
      method = 'POST';

      _metadata = {
          'name': fileName,
          'mimeType': 'application/pdf',
          'parents': [zzzz]
      };

    //blob is the data we receive in backend from _fContent variable
    var file = new Blob([blob], { type: 'application/pdf' });

    var accessToken = gapi.auth.getToken().access_token;
    var form = new FormData();
    form.append('metadata', new Blob([JSON.stringify(_metadata)], { type: 'application/json' }));
    form.append('file', file);

    var xhr = new XMLHttpRequest();
    xhr.open(method, endpoint);
    xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
    xhr.responseType = 'json';
    xhr.onload = () => {

        GapiUploadCallback(xhr.response, responseReturn);

    };
    xhr.send(form);

This is what i receive in google drive API, an empty/blank PDF file. Note: File size is 1 MB (1,424,457 bytes)

this is what i receive in google drive API, an empty PDF file

  • 1
    I think that your script works. So for example, how about confirming `blob` of `var file = new Blob([blob], { type: 'application/pdf' });` again? Or when `blob` is downloaded a PDF file, what result will you get? If this didn't lead to the direct solution, I apologize. – Tanaike Dec 08 '19 at 07:50
  • This is the blob result... the result is much longer. ``` %PDF-1.3 %���� 10 0 obj << /Type /XObject /Subtype /Image /Width 1240 /Height 1752 /BitsPerComponent 8 /ColorSpace /DeviceRGB /Filter [/FlateDecode /DCTDecode] /DecodeParms [null << /Quality 60 >>] /Length 145920 >> stream x���eT\��6�%����ܭp �n��Sx�܂������3�3f�����ow��Zϧ{��9g�}�}��S���$h�Q���P�Q���0p(�p��q�H�(X>���|ff�����eb��������!�g��b$�����N`���q�ph��h������7��� >................ ``` – Corodeanu Lucian Dec 08 '19 at 11:32
  • 1
    Thank you for replying. I'm glad your issue was resolved. – Tanaike Dec 08 '19 at 22:33

1 Answers1

0

Using javascript FileReader was the only option i could solve this. PDF's are rendering ok on Google Drive now.


 var fileUpload = $("#fileUpload").get(0);
    var files = fileUpload.files;

    var reader = new FileReader();
    reader.onload = function () {
        var dataURL = reader.result;

        var file = new Blob([dataURL], { type: 'application/pdf' });

        var accessToken = gapi.auth.getToken().access_token; // Here gapi is used for retrieving the access token.
        var form = new FormData();
        form.append('metadata', new Blob([JSON.stringify(_metadata)], { type: 'application/json' }));
        form.append('file', file);

        var xhr = new XMLHttpRequest();
        xhr.open(method, endpoint);
        xhr.setRequestHeader('Authorization', 'Bearer ' + accessToken);
        xhr.responseType = 'json';
        xhr.onload = () => {

            GapiUploadCallback(xhr.response, responseReturn);

        };
        xhr.send(form);
    };
    reader.readAsArrayBuffer(files[0]);