Reputation: 12214
I need to allow the user to download an array of bytes I get from a REST api.
The backend api return something like this:
GET /api/files/123
{
filename: 'myfile.pdf',
file: [base64]
}
On my html I have something like this:
<div ng-click="click()">Download</div>
And somewhere in my controller I have:
$scope.click: function (){
$http.get('/api/files/123',{headers:{x-security:'some_sec_token'}})
.then(
function (response){
// do something to return the array of bytes
},
function (error){
console.log(error);
}
);
}
I'm stuck on how to return the array of bytes with the Content-Disposition header using the filename returned by the api.
Upvotes: 1
Views: 1382
Reputation: 687
If you want to create a file using the array of bytes as content then you can use this library:
https://github.com/eligrey/FileSaver.js/blob/master/FileSaver.js
Import that library in you angular project and then try this:
var blob = new Blob(<arrayOfBytes>, {type: <yourFileType>});
saveAs(blob, [nameToSave]);
yourFileType should be something like "image/jpg" or similar depending on your file type
nameTosave should have the file type as well.. example: "myFile.pdf"
Hope it helps.
Cheers
Upvotes: 2