Forum Discussion
Can't download PDF from rest API. PDF not supported error
EDIT: I was able to "fix" it by returning it as a JSON object and one of the JSON object elements was the base64 encoding of the file. Then I used these 2 methods to convert the base64 encoding to an array and output the file
public Base64ToArrayBuffer(Base64: any)
{
var binaryString = window.atob(Base64);
var binaryLen = binaryString.length;
var bytes = new Uint8Array(binaryLen);
for (var i = 0; i < binaryLen; i++)
{
var ascii = binaryString.charCodeAt(i);
bytes[i] = ascii;
}
return bytes;
}
public SaveByteArray(FileName: string, ByteArray: any, MimeType: string)
{
var blob = new Blob([ByteArray], { type: MimeType });
var Link = document.createElement('a');
Link.href = window.URL.createObjectURL(blob);
Link.download = FileName;
Link.click();
};
I have the following API call using aadHttpClientFactory which is calling an endpoint that returns a PDF byte array.
In the HttpClientOptions I'm setting the Content-Type to application/pdf and the Content-Disposition to attachment; filename="test.pdf".
When the API call runs the .post() method the res variable returns a 400 error that I put below. Is there somewhere in Azure or SharePoint where I need to allow the PDF content type? I have no issues with my API calls when I return JSON.
Also, I used Postman to test the endpoint using a bearer token and it works successfully and returns the byte array.
{"code":"400","status":"REQUEST_ERROR","errors":["Content type 'application/pdf;charset=UTF-8' not supported"]}
return new Promise<any>(async (resolve, reject) =>
{
Context.aadHttpClientFactory
.getClient(Settings.AZURE_SHAREPOINT_APP_REG_CLIENTID)
.then((client: any): void =>
{
client
.post(url, AadHttpClient.configurations.v1, HttpClientOptions)
.then((res: any) =>
{
resolve(res);
}).catch(error =>
{});
}).catch(error =>
{});
});