how to download a file?

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

how to download a file?

Post 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
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: how to download a file?

Post 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
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: how to download a file?

Post 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
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: how to download a file?

Post 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
Dirk Geppert
Posts: 282
Joined: Fri Sep 22, 2017 7:02 am

Re: how to download a file?

Post 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  
User avatar
Peter
Posts: 1086
Joined: Mon Feb 24, 2014 10:17 pm
Location: 127.0.0.1:9080
Contact:

Re: how to download a file?

Post 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
Post Reply