0

I am trying to upload an excel file to OneDrive using Microsoft Graph API. The excel file is uploaded, but the problem is the file is corrupted in OneDrive. When I download the excel file and compare the file size with the original excel file, the excel file downloaded from the OneDrive size is high. So I think the issue is when uploading the file its charset is converted to UTF-8.

export async function uploadFile(accessToken) {
  try {
    const client = getAuthenticatedClient(accessToken);
    var res;

    var file = document.getElementById("inputFile").files[0];
    var r = new FileReader();

    r.onloadend = async function (e) {
      let promise = new Promise((res, rej) => {
        setTimeout(() => res("upload success"), 3000)
      });

      var data = e.target.result;
      res = await client.api('/me/drive/root:/Test/Test.xlsx:/content')
        .put(data);

      let result = await promise;
    }

    r.readAsBinaryString(file);

    return res;
  }
  catch (err) {    
    return err;
  }
}

Even I tried to set the content-type and charset to ANSI, But still, the file is corrupted.

res = await client.api('/me/drive/root:/Test/Test.xlsx:/content')
        .headers("Content-Type", "text/plain; charset=windows-1252")
        .put(data);

Any idea why the file is corrupted ?

I am testing in Windows 10.

1
  • Hello, I have the same problem. Did you find the solution ? When I upload a file of 10Ko inside OneDrive it arrives at 15ko. I think that is a problem coming from the encoding... Mar 27, 2021 at 16:45

1 Answer 1

1

I'd suggest using readAsArrayBuffer to make sure you're pushing up the raw bytes that the service is expecting.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.