Page 1 of 1

How to save binary data from an httpCallback?

Posted: Wed May 22, 2024 2:34 pm
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?

Re: How to save a PDF?

Posted: Fri May 24, 2024 8:49 am
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

Re: How to save binary data from an httpCallback?

Posted: Sun May 26, 2024 4:58 pm
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?

Re: How to save binary data from an httpCallback?

Posted: Sun May 26, 2024 7:16 pm
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 ...

Re: How to save binary data from an httpCallback?

Posted: Mon May 27, 2024 5:59 pm
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