Page 1 of 1

how to download a file?

Posted: Tue Jan 02, 2018 12:20 pm
by Dirk Geppert
Hello, everyone,

I use SpiderBite and CGI for server client communication. This works very well.

Now I don't want to return text/JSON via CGI, but a binary file. I send them:

Code: Select all

  WriteCGIHeader (#PB_CGI_HeaderContentType,"application/octet, -stream")
  WriteCGIData (*mem, size)
If I call the link in the browser directly, it works. The file can be loaded and saved.

But how do this with SpiderBasic? If I want to start the download after a gadget click, nothing happens.
The browser receives the data, but does not open the Save File dialog.

Ciao Dirk

Re: how to download a file?

Posted: Tue Jan 02, 2018 4:58 pm
by Peter
Hi Dirk,

this example (located in the PB-Helpfile under WriteCGIData()) works for me:

Code: Select all

  If Not InitCGI() Or Not ReadCGI()
    End
  EndIf
  
  WriteCGIHeader(#PB_CGI_HeaderContentType, "application/octet-stream")
  WriteCGIHeader(#PB_CGI_HeaderContentDisposition, "attachment; filename=image.png", #PB_CGI_LastHeader)
  
  If ReadFile(0, #PB_Compiler_Home + "examples/sources/data/world.png")
    Size = Lof(0)
    *Buffer = AllocateMemory(Size)
    ReadData(0, *Buffer, Size) ; Read the whole file into the new allocated buffer
    
    WriteCGIData(*Buffer, Size) ; Write the whole buffer to the CGI output
    
    CloseFile(0)
  EndIf
Greetings ... Peter

Re: how to download a file?

Posted: Wed Jan 03, 2018 9:37 am
by Dirk Geppert
Hi Peter,

the CGI Part works fine. But I want, that the User can Download and Save the file.
I want that the same should happen when you click on the button as when you click on the link.

Code: Select all

 Procedure HttpGetEvent(Success, Result$, UserData)
  If Success
    Debug Result$
  Else
    Debug "HTTPRequest(): Error"
  EndIf
EndProcedure

Procedure Download()
  HTTPRequest(#PB_HTTP_Get, "https://www.spiderbasic.com/download/SpiderBasic-free.zip", "", @HttpGetEvent())
EndProcedure

If OpenWindow(0, 100, 200, 300, 300, "Download a file", #PB_Window_TitleBar)
  
  ButtonGadget(0, 10, 10, 250, 30, "Start Download SpiderBasic" )
  BindGadgetEvent(0, @Download(), #PB_EventType_LeftClick)
  
  TextGadget(1, 10, 60, 250, 20,"<a href='https://www.spiderbasic.com/download/SpiderBasic-free.zip'>Start Download SpiderBasic</a>")
EndIf  
Ciao Dirk

Re: how to download a file?

Posted: Wed Jan 03, 2018 10:00 am
by Peter
ok, i get this error:
Failed to load https://www.spiderbasic.com/download/Sp ... 4973122945: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://127.0.0.1:9080' is therefore not allowed access.
i suspect, that it can only be done with a PHP (or CGI) - Proxy.

for example this one:

http://www.abdulqabiz.com/blog/archives ... -requests/

Greetings ... Peter

Re: how to download a file?

Posted: Fri Jan 05, 2018 8:28 am
by Dirk Geppert
Hi Peter, I just remembered that with Pb2Web everything worked out, using RunProgram ()

Now I've found out, that it works in this way:

Code: Select all

Procedure Download()
  ! window.open("https://www.spiderbasic.com/download/SpiderBasic-free.zip");
EndProcedure

If OpenWindow(0, 100, 200, 300, 300, "Download a file", #PB_Window_TitleBar)
  ButtonGadget(0, 10, 10, 250, 30, "Start Download SpiderBasic" )
  BindGadgetEvent(0, @Download(), #PB_EventType_LeftClick)
EndIf  

Re: how to download a file?

Posted: Fri Jan 05, 2018 10:41 am
by Peter
Dirk Geppert wrote:Hi Peter, I just remembered that with Pb2Web everything worked out, using RunProgram ()
that's because PB2Web-RunProgram() calls window.open() ;-)

(see also: http://www.purebasic.fr/german/viewtopi ... 10#p341510)

Greetings ... Peter