Downloading file with different name?

Just starting out? Need help? Post your questions and find answers here.
jphoarau
Posts: 27
Joined: Thu Dec 28, 2023 4:30 am

Downloading file with different name?

Post 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.
jphoarau
Posts: 27
Joined: Thu Dec 28, 2023 4:30 am

Re: Downloading file with different name?

Post 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.
Fred
Site Admin
Posts: 1854
Joined: Mon Feb 24, 2014 10:51 am

Re: Downloading file with different name?

Post 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.
jphoarau
Posts: 27
Joined: Thu Dec 28, 2023 4:30 am

Re: Downloading file with different name?

Post by jphoarau »

Thank you. I'm going to do that.
Fred
Site Admin
Posts: 1854
Joined: Mon Feb 24, 2014 10:51 am

Re: Downloading file with different name?

Post by Fred »

I changed it for the next version.
jphoarau
Posts: 27
Joined: Thu Dec 28, 2023 4:30 am

[Solved] Re: Downloading file with different name?

Post 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.
Ken
Posts: 15
Joined: Sat Dec 19, 2015 6:28 pm

Re: Downloading file with different name?

Post 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>
jphoarau
Posts: 27
Joined: Thu Dec 28, 2023 4:30 am

Re: Downloading file with different name?

Post by jphoarau »

Thank you very much. I will test it. It seems more simple that mine.
Post Reply