Topics   All   Mac OS X (Only)   Windows (Only)   Linux (Only, Not)   iOS (Only, Not)  
Components   Crossplatform Mac & Win   Server (Not)   Client   Old   Guides   Examples
New in version: 6.1   6.2   6.3   6.4   6.5   7.0   7.1   7.2   7.3   7.4    Statistic  

CURL.CreateOutputFile

Creates output file for downloading data.

Component Version macOS Windows Server FileMaker Cloud FileMaker iOS SDK
CURL 2.6 Yes Yes Yes Yes Yes

MBS( "CURL.CreateOutputFile"; Handle; Path )

Parameters

Parameter Description Example value
Handle The CURL session handle. $curl
Path Native file path. C:\Documents\Test.Txt

Result

Returns "OK" on success or error message.

Description

Creates output file for downloading data.
If you set MBS( "CURL.SetOptionFileOnlyMode"; $curl; 1 ), the data is only written to file and not available with CURL.GetResult* functions. If creation of the file fails, you get an error message back, but still your transfer will probably succeed.
Do not call this method while a transfer is running.

This function requires a native path. Use Path.FileMakerPathToNativePath to convert a FileMaker path to a native path if required. If you like to have the user choose the path, you can use FileDialog functions.
For Server be aware that server has limited permissions and may not be able to access all files on a computer.

Examples

Write data to a file on Mac in temp folder:

MBS( "CURL.CreateOutputFile"; $curl; "/tmp/download.dat" )

Write data only to file on Windows:

MBS( "CURL.SetOptionFileOnlyMode"; $curl; 1 )
MBS( "CURL.CreateOutputFile"; $curl; "c:\download.dat" )

Download URL to temp file:

Set Variable [$curl; Value:MBS("CURL.New")]
#this is URL to download
Set Variable [$url; Value:"http://www.monkeybreadsoftware.de/filemaker/test.txt"]
#get file name from URL
Set Variable [$name; Value:MBS( "Path.LastPathComponent"; $URL )]
#now make temp file path
Set Variable [$path; Value:MBS( "Path.AddPathComponent"; MBS( "Folders.SystemTemporary" ); $name )]
#set URL to download
Set Variable [$r; Value:MBS("CURL.SetOptionURL"; $curl; "http://www.monkeybreadsoftware.de/filemaker/test.txt")]
#open destination file
Set Variable [$r; Value:MBS("CURL.CreateOutputFile"; $curl; $path)]
#run transfer
Set Field [CURL Test::Result; MBS("CURL.Perform"; $curl)]
#close destination file
Set Variable [$r; Value:MBS("CURL.CloseOutputFile"; $curl)]
#get debug messages
Set Field [CURL Test::debug; MBS("CURL.GetDebugAsText"; $curl)]
Set Variable [$result; Value:MBS("CURL.Cleanup"; $curl)]

Download file via SFTP to file on disk:

Set Variable [$curl; Value:MBS("CURL.New") ]
# set download URL, e.g. sftp://monkeybreadsoftware.net/test.jpg
Set Variable [$result; Value:MBS("CURL.SetOptionURL"; $curl; SFTP Download::URL )]
# build file path on disk and open output file
Set Variable [$name; Value:GetValue(SFTP Download::FileName; 1)]
Set Variable [$path; Value:MBS( "Path.AddPathComponent"; SFTP Download::FolderPath; $name )]
Set Variable [$r; Value:MBS("CURL.CreateOutputFile"; $curl; $path)]
# login only via password
Set Variable [$result; Value:MBS( "CURL.SetOptionSSHAuthTypes"; $curl; 2+8 )]
# set credentials
Set Variable [$result; Value:MBS("CURL.SetOptionUsername"; $curl; SFTP Download::UserName )]
Set Variable [$result; Value:MBS("CURL.SetOptionPassword"; $curl; SFTP Download::Password )]
# run transfer
Set Field [SFTP Download::ErrorCode; MBS("CURL.Perform"; $curl)]
# check debug log for errors
Set Field [SFTP Download::Debug; MBS("CURL.GetDebugAsText"; $curl)]
# free CURL session
Set Variable [$result; Value:MBS("CURL.Cleanup"; $curl)]

Batch download a lot of files via FTP/SFTP:

# URL field contains ftp or sftp URL with / on end.
# FileList field contains list of files to load
# OutputPath field contains folder path on disk
#
Set Variable [$i; Value:1]
Set Variable [$max; Value:ValueCount ( SFTP Download::FileList )]
#
Set Variable [$curl; Value:MBS("CURL.New")]
#
# login only via password
Set Variable [$result; Value:MBS( "CURL.SetOptionSSHAuthTypes"; $curl; 2+8 )]
#
# set credentials
Set Variable [$result; Value:MBS("CURL.SetOptionUsername"; $curl; SFTP Download::UserName )]
Set Variable [$result; Value:MBS("CURL.SetOptionPassword"; $curl; SFTP Download::Password )]
#
# start loop over file lists
Loop
    #
    Set Variable [$name; Value:GetValue ( SFTP Download::FileList ; $i )]
    #
    # build file path on disk and open output file
    Set Variable [$path; Value:MBS( "Path.AddPathComponent"; SFTP Download::OutputPath; $name )]
    Set Variable [$r; Value:MBS("CURL.CreateOutputFile"; $curl; $path)]
    #
    # set URL with file name
    Set Variable [$result; Value:MBS("CURL.SetOptionURL"; $curl; SFTP Download::URL & $name )]
    #
    # run transfer
    Set Field [SFTP Download::ERGEBNIS; MBS("CURL.Perform"; $curl)]
    #
    # query debug log
    Set Field [SFTP Download::DEBUG; MBS("CURL.GetDebugAsText"; $curl)]
    #
    Set Variable [$i; Value:$i +1]
    Exit Loop If [$max = $i]
    #
End Loop
Set Variable [$result; Value:MBS("CURL.Cleanup"; $curl)]

See also

Example Databases


CURL.CreateHeaderOutputFile   -   CURL.ErrorCode

Feedback: Report problem or ask question.




Links
MBS Xojo blog