Skip to main content
Answer

download PDF from an API in a button

  • June 6, 2023
  • 4 replies
  • 248 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

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

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
  • 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
  • June 7, 2023

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