When getting the file data for an excel document using getFileAsync the data is corrupt

geetarman 26 Reputation points
2024-04-05T07:04:47.01+00:00

I am using the code in the following link

https://learn.microsoft.com/en-us/javascript/api/office/office.filetype?view=common-js-preview

to get the contents of a file to send it to an web service to analyse it. However, the data that is created is corrupt. It looks very much like the encoding is incorrect but I have been unable to correct it. I have tried using the byte array rather than the string as created in this example but it still looks wrong.

function onGotAllSlices(docDataSlices)

{

let docData = [];

for (let i = 0; i < docDataSlices.length; i++) { docData = docData.concat(docDataSlices[i]); }

let fileContent = new String();

for (let j = 0; j < docData.length; j++)

{

fileContent += String.fromCharCode(docData[j]);

}

// Now all the file content is stored in 'fileContent' variable,

// you can do something with it, such as print, fax...

// When I use this fileContent string, send it to web serverice and save it to a file the file is corrupt

// eg even when saved for debugging using the following code

//// Creating a Blob

const blob = new Blob([longString], { type: 'text/plain' });

// Creating a temporary link   

const link = document.createElement('a');

link.download = 'document16.xlsx';

link.href = window.URL.createObjectURL(blob);

link.click();
```}

Does anyone have any ideas how to fix this.
JavaScript API
JavaScript API
An Office service that supports add-ins to interact with objects in Office client applications.
871 questions
0 comments No comments
{count} votes