SendFile Method

Syntax

Context.Response.SendFile as V (Filename as C, PromptToSave as L = .F., OutputFilename as C = "__not__specified__")

Arguments

FilenameCharacter

The name of the file to written to the response's output.

PromptToSaveLogical

Add the Content-Disposition header to the client will prompt for save. This defaults to .F. when an OutputFilename is not specified. It defaults to .T. when an OutputFilename is specified.

OutputFilenameCharacter

Default = "__not__specified__". The name the client should use when saving the file. This is the filename shown in the save file dialog. The user can change the filename at their discretion when they save the file on the client.

Description

Write the content of a file to output stream

Discussion

Context.Response.SaveFile() Write the content of a file directly to the response's output stream.

For example, the code below creates a file called "MyFile.txt" and sends it to the client where it will either be displayed in the browser or the user will be prompted to download the file.

dim FileKey as c = "MyFile.txt"
context.session.SaveDataAsFile(FileKey,"Hello World, this is a session file")

context.response.SendFile(Session.FormatFileDataURL(FileKey))

If you want to prompt the user to download the file, you can pass .t. for the second parameter. When PromptToSave is true, the user will be asked to save the file. You can optionally specify a default filename to show in the save dialog as the third parameter. If you do not specify an OutputFilename, the server filename is used.

context.response.SendFile(Session.FormatFileDataURL(FileKey), .t., "HelloWorld.txt")

Sending Files from Ajax Callbacks

The sendFile() method cannot be used to send a file from an Ajax Callback in a component. Instead, use a5Helper_generateFileDownloadJS(). For example:

function genExcelFile as c (e as p)

    dim cn as SQL::Connection
    dim sql as C = "SELECT * FROM Customers"
    dim filename as c = context.request.GetRequestTempFileName(".xlsx")
    dim js as c = ""

    ' Open connection to Northwind database
    cn.open("::Name::AADemo-Northwind")
    
    if (cn.toExcel(sql, filename, "Customers")) then
        js = a5Helper_generateFileDownloadJS(e.tmpl.alias,filename,"Northwind_Customers.xlsx")
    else
        dim cr as SQL::CallResult = cn.CallResult

        trace.writeLn("An error occurred in genExcelFile(): " + cr.text)
    end if
    cn.close()

    return js

end function

See a5Helper_generateFileDownloadJS() for more info.

Limitations

Cannot be used in Ajax Callbacks

See Also