Sharepoint REST API Download File Using NodeJs
-
22-01-2021 - |
Question
i'm trying to save file to my drive using NodeJS as backend. So far, i managed to access
https://${sites}/_api/web/GetFolderByServerRelativeUrl('Shared%20Documents')/Files('test.xlsx')/$value
and get the return like;
so far, no error message appears and i assume the request to the above url is succeed. what can i do with this data? i tried to use fs.write()
but then when i open the file, it corrupts. any response from you guys is highly appreciated.
thanks.
Note: i tried using this method Download sharepoint document with REST as binary file not working for me but still didn't work.
Solution 2
i've found out the solution for my case based on this thread :
https://stackoverflow.com/questions/5294470/writing-image-to-local-server
everything works just fine.
thanks.
OTHER TIPS
There are some libraries available for such a task. For example consider sppull.
With sppull
you can easily download file with just a few lines of code:
var sppull = require("sppull").sppull;
var context = {
siteUrl: "http://contoso.sharepoint.com/subsite",
creds: {
username: "user@contoso.com",
password: "_Password_"
}
};
var options = {
spRootFolder: "Shared%20Documents/Contracts",
dlRootFolder: "./Downloads/Contracts"
};
sppull(context, options)....
Also check out sp-download