Page 1 of 1

Downloading file with different name?

Posted: Mon Jan 05, 2026 11:46 am
by jphoarau
Hello,

I would like to download a file and save it to the user with a different name than the one on the server. I'm doing this:

Code: Select all

Procedure OnLeftClick_OK()
  If( GetGadgetState( #Checkbox_0 ) = #PB_Checkbox_Checked )
    ;procedure to download moustique
    Debug "Downloading book."
    If( ReadFile(0, "moustique.pdf", @Callback) )
      Length.q = Lof(0)
      *Buffer = AllocateMemory(Length);Error here: RangeError: invalid or out-of-range index (http://127.0.0.1:9080/spiderbasic.js?t=1767612810, line: 149)
      *Buffer = ExportFileMemory(0)
      ;ReadData(0, *Buffer, 0, Length)
      CreateFile(1, "mosticos.pdf", @Callback)
      WriteData(1, *Buffer, 0, Length);There is an error in the documentation for this function. Only 3 parameters are given when 4 are required. Perhaps this should be reported?
      ExportFile(1, "application/pdf")
      CloseFile(0)
      CloseFile(1)
    ;ExportFile("moustique.pdf", "application/pdf")
    EndIf
  EndIf
EndProcedure
I'm getting this error: RangeError: invalid or out-of-range index (http://127.0.0.1:9080/spiderbasic.js?t=1767612810, line: 149)

What is my mistake? Thanks in advance.

Re: Downloading file with different name?

Posted: Wed Jan 07, 2026 6:26 am
by jphoarau
I have found how to download a file, but I don't know how to rename it. I do that (based on example):

Code: Select all

Procedure CreateFileCallback(Status, Filename$, File, SizeRead)
  Select Status
    Case #PB_Status_Saved
    
    Case #PB_Status_Error
    
  EndSelect
EndProcedure

Procedure ReadFileCallback(Status, Filename$, File, SizeRead)
  Select Status
    Case #PB_Status_Loaded
      ExportFile(File, "application/pdf")
      CloseFile(File)
      
    Case #PB_Status_Error
      
  EndSelect
EndProcedure

ReadFile(0, "moustique.pdf", @ReadFileCallback())
How could I do to change the name of the file saved on user side? Thanks in advance.

Re: Downloading file with different name?

Posted: Thu Jan 08, 2026 9:06 am
by Fred
I don't think you can do that easily, I could add an optional output name for ExportFile(). For now, you can try to create another file with the good filename and use WriteData() to transfert it. Not very simple I agree.

Re: Downloading file with different name?

Posted: Thu Jan 08, 2026 9:39 am
by jphoarau
Thank you. I'm going to do that.

Re: Downloading file with different name?

Posted: Thu Jan 08, 2026 10:06 am
by Fred
I changed it for the next version.