I'm trying to get and save files from SharePoint app using REST, the files are located inside the host SP.
Once got the file, I'm saving/downloading it into the browser with a save function.
When I download a .txt, I get file and contents downloaded and saved. When I download a .pdf or .doc I get a file but once saved the content is empty. The new created file as the correct extension a size but it's like I'm not getting the correct binary for .pdf and .doc files, any suggestion?
Here is my code:
var prepareUrl = appweburl + "/_api/SP.AppContextSite(@target)/web/GetFileByServerRelativeUrl('" + filePath + "')/$value?@target='" + hostweburl + "'";
// executes cross domain request
var executor = new SP.RequestExecutor(appweburl);
executor.executeAsync(
{
url: prepareUrl,
type: "GET",
binaryStringResponseBody: true,
success: function (data) { readContentsAndSendToIAG(data, fileName) },
error: function (xhr) {
alert(xhr.status + ": " + xhr.statusText)
}
});
}
function readContentsAndSendToIAG(data, fileName) {
var blob = new Blob([data.body], { type: "application/pdf" });
saveAs(blob, fileName);
Thanks.