How to save binary data from an httpCallback?

Just starting out? Need help? Post your questions and find answers here.
Dirk Geppert
Posts: 332
Joined: Fri Sep 22, 2017 7:02 am

How to save binary data from an httpCallback?

Post by Dirk Geppert »

Hi folks,

if I want to download a document from a server with Spiderbasic, I always have to use

Code: Select all

Procedure RunProgram(Filename.s, Parameter.s = "")
  !  if (v_parameter != "") {
  !    var win = window.open(v_filename, v_parameter);
  !    win.focus();
  !  } else {
  !    window.open(v_filename);
  !  }
EndProcedure

RunProgram ("https:/myServer.org/cgi-bin/cgi.exe?0=CreatePDF", "")

But how can I save a PDF that I have received via a callback?
The Result.s contains the PDF. But how can I save this?

Code: Select all

Procedure CGI_CreatePDFCallback(success, Result.s)
  If CreateFile(0, "myDocument.pdf", 0)
    WriteString(0, Result)
    ExportFile(0, "application/pdf", #PB_LocalFile)
    CloseFile(0)
  EndIf
EndProcedure
A PDF is saved, but it is incorrect. May be because I treat the binary data as a string.
I must be using WriteData(). But how do I get Result.s into an allocated memory?

Can anyone help me?
Last edited by Dirk Geppert on Sun May 26, 2024 5:10 pm, edited 1 time in total.
User avatar
bembulak
Posts: 95
Joined: Wed Feb 26, 2014 9:53 am

Re: How to save a PDF?

Post by bembulak »

I'm unable to test, but what about ExportFileMemory(#File)?

From Help:

Code: Select all

 If CreateFile(0, "SomeText.txt")
    WriteStringN(0, "First line")
    WriteStringN(0, "Second line")
    
    *Buffer = ExportFileMemory(0)
    Debug PeekS(*Buffer, 0, 3) ; Will display 'Fir'
    
    CloseFile(0)
  EndIf
Kind regards,

bembulak
Dirk Geppert
Posts: 332
Joined: Fri Sep 22, 2017 7:02 am

Re: How to save binary data from an httpCallback?

Post by Dirk Geppert »

Thx bembulak. Interesting, I didn't even know the ExportFileMemory command.

However, I would need exactly the equivalent: Something like ImportFileMemory - which writes data from a memory buffer to a file.

But let me explain again. If I send a post with HttpRequest and then receive binary data (e.g. a PDF document) from the server.

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData) ; Result$ contains binary data
    If Success
      Debug Result$
    Else
      Debug "HTTPRequest(): Error"
    EndIf
  EndProcedure
  
  ; Get the content of this file, and display it in the debug window
  ;
HTTPRequest(#PB_HTTP_Post, "myDocumentServer", "payload", @HttpGetEvent())
How can I save this data in a file?
User avatar
bembulak
Posts: 95
Joined: Wed Feb 26, 2014 9:53 am

Re: How to save binary data from an httpCallback?

Post by bembulak »

Dirk Geppert wrote: Sun May 26, 2024 4:58 pm Thx bembulak. Interesting, I didn't even know the ExportFileMemory command.

However, I would need exactly the equivalent: Something like ImportFileMemory - which writes data from a memory buffer to a file.

But let me explain again. If I send a post with HttpRequest and then receive binary data (e.g. a PDF document) from the server.

Code: Select all

Procedure HttpGetEvent(Success, Result$, UserData) ; Result$ contains binary data
    If Success
      Debug Result$
    Else
      Debug "HTTPRequest(): Error"
    EndIf
  EndProcedure
  
  ; Get the content of this file, and display it in the debug window
  ;
HTTPRequest(#PB_HTTP_Post, "myDocumentServer", "payload", @HttpGetEvent())
How can I save this data in a file?
I don't know a native SB solution, to be honest.
Have you tried inline JS? A quick search gave me the following:
https://stackoverflow.com/questions/648 ... i-response

Unfortunately, I'm not firm with JS at all ...
Kind regards,

bembulak
User avatar
bembulak
Posts: 95
Joined: Wed Feb 26, 2014 9:53 am

Re: How to save binary data from an httpCallback?

Post by bembulak »

I'm eager to play with this. Do you have a link at hand, I can use? Or is there some sort of service out there, that does the same?

Here's another tutorial I've found:
https://medium.com/@storrisi/how-to-sho ... 220fee60cb
Kind regards,

bembulak
Post Reply