4

First of all, i am using node.js with provider hosted app with elevated rights to do this, no front-end at all.

I just want to download a file using the API and then save it. I can't save if the API response isn't in binary format (or at least i cant save with the response that gives me).

Using the API endpoint:

https://{tennant}.sharepoint.com/{site}/_api/Web/GetFileByServerRelativeUrl('{relativepathtofile}')/$value

with the header option binaryStringResponseBody: true

or

https://{tennant}.sharepoint.com/{site}/_api/Web/GetFileByServerRelativeUrl('{relativepathtofile}')/OpenBinaryStream

doesn't return me the content in binary. It returns me the content but not binary.

With some type of documents (.docx) this doesn't let me to rebuild the file and save into the disc, so is a very high trouble for me at the moment.

I hope someone can bring me some light to be able to download the file as binary or whatever mode but not like it actually returns me the file.

Regards

3

Ok i found the problem...

First: The API calls were both correct.

The fact is that I am developing with Nodejs and I realized that the request module automatically converts the result to utf8 as the documentation says:

encoding - Encoding to be used on setEncoding of response data. If null, the body is returned as a Buffer. Anything else (including the default value of undefined) will be passed as the encoding parameter to toString() (meaning this is effectively utf8 by default). (Note: if you expect binary data, you should set encoding: null.)

So using the encoding: null option it is working fine :)

EDIT:

I am going to edit the answer (TypeScript) with the request options to help someone else that faces the same problem. Btw i think i didn't mention before but is for SharePoint Online:

    let options: request.Options = { 
        method: "GET",
        uri: `https://${tennant}.sharepoint.com/${site}/_api/web/GetFileByServerRelativeUrl('${serverRelativeUrl}')/$value`, //  OpenBinaryStream also works
        headers: {
            "Authorization": "Bearer " + accessToken,
            "accept": "application/json; odata=verbose",
        },
        encoding: null
    };

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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