Storing files for download on FileMaker Server with download URLs
As you may know the FileMaker Server comes with a copy of apache web server software. You can use the apache web server to host and deliver files for download. So if you copy a file to /Library/FileMaker Server/HTTPServer/htdocs/ folder on your server, you can than point to that file with the URL of your server with folder and file name attached.
We can call the following script named "Create URL". It calls a script on the server to export the picture of our current record and return the URL. As server doesn't retain the context, we need to pass the record ID fo the current record as parameter:
Perform Script on Server [ Specified: From list ; “Create URL on Server” ; Parameter: Get(RecordID) ; Wait for completion: On ]
Set Variable [ $URL ; Value: Get(ScriptResult) ]
Set Field [ Anlagen::Name ; $URL ]
The script Create URL on Server looks like this:
# go to record on Server
Set Variable [ $RecordID ; Value: Get(ScriptParameter) ]
Go to Layout [ “Images” (Anlagen) ; Animation: None ]
If [ Get(LastError) ≠ 0 ]
Exit Script [ Text Result: "" // failed ]
End If
Go to Record/Request/Page [ With dialog: Off ; $RecordID ]
If [ Get(LastError) ≠ 0 ]
Exit Script [ Text Result: "" // failed ]
End If
#
# Global settings
Set Variable [ $path ; Value: "/Library/FileMaker Server/HTTPServer/htdocs/" ]
Set Variable [ $URL ; Value: "http://127.0.0.1/" ]
Set Variable [ $folderName ; Value: Get(UUID) ]
#
# Item to export
Set Variable [ $Container ; Value: Anlagen::Bild ]
Set Variable [ $FileName ; Value: MBS( "Path.LastPathComponent"; GetAsText($Container)) ]
#
# Create folder
Set Variable [ $FolderPath ; Value: MBS( "Path.AddPathComponent"; $path; $FolderName ) ]
Set Variable [ $r ; Value: MBS( "Files.CreateDirectory"; $FolderPath ) ]
If [ MBS("IsError") ]
Exit Script [ Text Result: "" // failed ]
End If
#
# Export file
Set Variable [ $FilePath ; Value: MBS( "Path.AddPathComponent"; $FolderPath; $FileName ) ]
Set Variable [ $r ; Value: MBS( "Container.WriteFile"; $Container; $FilePath ) ]
If [ MBS("IsError") ]
Exit Script [ Text Result: "" // failed ]
End If
#
# Return URL
Set Variable [ $FileNameURL ; Value: MBS( "Text.EncodeURLComponent"; $FileName; "UTF8" ) ]
Set Variable [ $result ; Value: $URL & $FolderName & "/" & $FileNameURL ]
Exit Script [ Text Result: $result ]
As you see, we create a new folder with an UUID as name, so every user has an unique folder. We write the file there and build the URL with the encoded file name. Now the client script has the URL and you can give it the user, e.g. in an email to download later.
It may be good to put all those downloads in an extra folder so the folder is not filled too much. Also we could store the folder names in a table with timestamp, so we can remove them after e.g. 30 days.