Santhosh
Santhosh

Reputation: 69

Excel file Corrupted while uploading to OneDrive #MicrosoftGraph

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.

Upvotes: 0

Views: 336

Answers (1)

Brad
Brad

Reputation: 4192

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

Upvotes: 1

Related Questions