Skip to main content
Solved

download PDF from an API in a button

  • June 6, 2023
  • 4 replies
  • 246 views

abdallaahmed61
Varsity III
Forum|alt.badge.img+1

I have an API that returns a PDF file in the content 

is there a way that i can make a button that consumes this api and return the file in the browser to be downloaded ?

the PDF file is big so i will need to await it

Best answer by abdallaahmed61

i solved the problem by using Task.Run and Task.Wait
and used the 
throw new PXRedirectToFileException(new FileInfo("downloaded.pdf", null, memoryStream.ToArray()), true);
that zfebert56 mentioned

4 replies

Zoltan Febert
Jr Varsity I
Forum|alt.badge.img+3
  • Jr Varsity I
  • 177 replies
  • June 7, 2023

If you downloaded the file into a MemoryStream, you can use this code:

throw new PXRedirectToFileException(new FileInfo("downloaded.pdf", null, memoryStream.ToArray()), true);

 


abdallaahmed61
Varsity III
Forum|alt.badge.img+1
  • Author
  • Varsity III
  • 53 replies
  • June 7, 2023

If you downloaded the file into a MemoryStream, you can use this code:

throw new PXRedirectToFileException(new FileInfo("downloaded.pdf", null, memoryStream.ToArray()), true);

 

this may solve the second problem 
but i still need to fix the first problem which is, i cant make the button async to wait the api call.


abdallaahmed61
Varsity III
Forum|alt.badge.img+1
  • Author
  • Varsity III
  • 53 replies
  • Answer
  • June 7, 2023

i solved the problem by using Task.Run and Task.Wait
and used the 
throw new PXRedirectToFileException(new FileInfo("downloaded.pdf", null, memoryStream.ToArray()), true);
that zfebert56 mentioned


Chris Hackett
Community Manager
Forum|alt.badge.img
  • Acumatica Community Manager
  • 3296 replies
  • June 7, 2023

Thank you for sharing your solution with the community @abdallaahmed61 !