Guest User

Untitled

a guest
Feb 18th, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. async downloadFile(file: UploadedFileMetaData) {
  2. if (this.downloading.has(file)) {
  3. return;
  4. }
  5.  
  6. this.downloading.add(file);
  7.  
  8. let url: string;
  9.  
  10. try {
  11. url = await this.blobStorageService.getDownloadUrl(file);
  12. } catch (err) {
  13. throw err;
  14. }
  15.  
  16. return this.http
  17. .get(url, {
  18. responseType: ResponseContentType.Blob
  19. })
  20. .map((res) => {
  21. return {
  22. filename: file.blobName,
  23. data: res.blob()
  24. };
  25. })
  26. .subscribe(
  27. (res) => {
  28. console.log('start download:', res);
  29. const fileUrl = window.URL.createObjectURL(res.data);
  30. const a = document.createElement('a');
  31. document.body.appendChild(a);
  32.  
  33. a.setAttribute('style', 'display: none');
  34.  
  35. a.href = fileUrl;
  36.  
  37. a.download = res.filename;
  38. a.click();
  39.  
  40. window.URL.revokeObjectURL(fileUrl);
  41. a.remove();
  42. },
  43. (error) => {
  44. this.downloading.delete(file);
  45. console.log('download error:', JSON.stringify(error));
  46. },
  47. () => {
  48. this.downloading.delete(file);
  49. console.log('Completed file download.');
  50. }
  51. );
  52. }
Add Comment
Please, Sign In to add comment