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.
[Solved] Re: Downloading file with different name?
Posted: Sat Jan 10, 2026 3:55 am
by jphoarau
I have solved the problem to rename a file. Here is the code below:
Code: Select all
Enumeration
#File1
#File2
EndEnumeration
Global *Buffer
Global Length.q
Procedure ReadCallback( Status, Filename$, File, SizeRead )
Select Status
Case #PB_Status_Loaded
*Buffer = ExportFileMemory(#File1)
Length = Lof(#File1)
Debug Length
WriteData(#File2, *Buffer, 0, Length)
ExportFile(#File2, "application/pdf")
CloseFile(#File2)
CloseFile(#File1)
Case #PB_Status_Error
Debug "File not found in localstorage: " + Filename$
; Debug "Creating a new file..."
EndSelect
EndProcedure
Procedure WriteCallback(Status, Filename$, File, SizeRead)
Select Status
Case #PB_Status_Saved
; File correctly saved
; WriteData(#File2, *Buffer, 0, Length);Why it doesn't work here?
; ExportFile(#File2, "application/pdf")
; CloseFile(#File2)
; CloseFile(#File1)
Case #PB_Status_Error
; File saving has failed
EndSelect
EndProcedure
Procedure Rename()
;procedure to download moustique
Debug "Downloading book."
If( ReadFile(#File1, "moustique.pdf", @ReadCallback()) )
Debug #File1
EndIf
If( CreateFile(#File2, "mosticos.pdf", @WriteCallback()))
Debug #File2
EndIf
EndProcedure
Rename()
It works fine, but I don't know how.
The Debug output show:
Downloading book.
0
1
34525
So, I have a question: why the program runs line 50 (Debug #File2) before line 14 (Debug Length)? And why can't I write lines 31 to 34?
And also, why lines 15 to 18 works well at theses places and not in the WriteCallback Procedure?
This is just 2 questions for my knowledge only, because it works for now.
Thanks in advance for your answer.
Re: Downloading file with different name?
Posted: Mon Jan 12, 2026 6:51 pm
by Ken
Here is solution what Fred suggested.
Code: Select all
;Download file from server and save with different name
Global *FileBuffer
Global FileSize
; Callback for CreateFile (when file is saved)
Procedure CreateFileCallback(Status, Filename$, File, SizeRead)
Select Status
Case #PB_Status_Saved
Debug "File saved: " + Filename$
Case #PB_Status_Error
Debug "Error saving file: " + Filename$
EndSelect
If *FileBuffer
FreeMemory(*FileBuffer)
EndIf
EndProcedure
; Callback when remote file is loaded
Procedure ReadRemoteFileCallback(Status, Filename$, File, Size)
Select Status
Case #PB_Status_Loaded
Debug "Remote file loaded: " + Filename$ + " (" + Str(Size) + " bytes)"
; Allocate buffer and read all data
FileSize = Lof(File)
*FileBuffer = AllocateMemory(FileSize)
ReadData(File, *FileBuffer, 0, FileSize)
CloseFile(File)
; Create NEW file with DESIRED filename (requires callback!)
If CreateFile(1, "MyNewFilename.txt", @CreateFileCallback())
WriteData(1, *FileBuffer, 0, FileSize)
ExportFile(1, "application/octet-stream") ; Triggers download to user
CloseFile(1)
Debug "File exported as: MyNewFilename.txt"
EndIf
Case #PB_Status_Error
Debug "Error loading remote file: " + Filename$
EndSelect
EndProcedure
; Download file from server (remote URL)
ReadFile(0, "https://yourserver.com/original-filename.txt", @ReadRemoteFileCallback())
If you test note possible CORS problems.
In Apache you can use for example:
Code: Select all
<FilesMatch "\.(txt|pdf|zip)$">
Header set Access-Control-Allow-Origin "*"
</FilesMatch>
Re: Downloading file with different name?
Posted: Mon Jan 12, 2026 7:26 pm
by jphoarau
Thank you very much. I will test it. It seems more simple that mine.
Re: Downloading file with different name?
Posted: Tue Jan 13, 2026 4:15 pm
by Fred
With the 3.20 final, you have an optional parameter for the name.
Re: Downloading file with different name?
Posted: Tue Jan 13, 2026 7:13 pm
by jphoarau
Thank you a lot. I will try it. But your little code is working fine.