Storing Files in Sessions


Session storage is able to deliver robust and scalable server solutions. In order to enable this feature, data intended to be stored on disk at the session level should use context.session.SaveDataAsFile(). This stored file can then be linked to using context.session.FormatFileDataURL().

context.session.SaveDataAsFile() is a method that takes a key and data, then saves that data somewhere using the key you supplied. The key is typically a filename without a path but with an extension, for instance as MyFile.txt. The storage location is not significant to the application developer because you will not be directly coding to the physical location.

When you want to link to a file that has been stored using context.session.SaveDataAsFile(), you use context.session.FormatFileDataURL() to create the URL by passing in the key you used when storing the data.

dim FileKey as c = "MyFile.txt"
context.session.SaveDataAsFile(FileKey,"Hello World, this is a session file")
The file extension of the filename used as a key in context.session.SaveDataAsFile(Key, data) and context.session.FormatFileDataURL(Key) matters, because the extension is used for MIME type parsing. For example, in order to have Excel data treated as such when it is sent to the browser, use an extension of .XLS. The above example uses a file extension of .TXT.

Generating Temporary File Names 

Temporary file names are used when generating reports and other files on the server that will be either sent directly to a user for download or moved to a long-term storage system, such as a database or Amazon S3. Functions, such as a5w_report_saveAs() require the file name before the file can be created. In this situation, you can use the context.request.GetRequestTempFileName() method to generate a temporary location where the file will be created. The file can then be moved to session storage or other location.

dim filter as c = ""
dim order as c = ""
Dim filename as c = context.request.GetRequestTempFileName()
a5w_report_saveAs(context.request.applicationRoot + "MyReport.a5rpt","pdf",filter,order,filename)
if (file.exists(filename)) then
    ' Code to send file to user, save in a database, or upload to S3.
end if

Sending Files to the Client 

Files can be sent back to the client browser using the context.response.SendFile() method. The file will be sent to the user's browser. The user will either be prompted to download the file or be shown the file. Using context.response.SendFile() is a Best Practice and is strongly recommended over other, less efficient methods, such as context.response.Redirect():

dim filter as c = ""
dim order as c = ""
Dim filename as c = context.request.GetRequestTempFileName()
a5w_report_saveAs(context.request.applicationRoot + "MyReport.a5rpt","pdf",filter,order,filename)
if file.exists(filename)
    ' Prompt user to save file with alternate file name:
end if

Sending Files from Ajax Callbacks 

The context.response.sendFile() method cannot be used to send a file from an Ajax Callback in a component. Instead, use a5Helper_generateFileDownloadJS(). a5Helper_generateFileDownloadJS() will generate the required JavaScript to send to the client to download the file. 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")
    ' Open connection to Northwind database"::Name::AADemo-Northwind")
    if (cn.toExcel(sql, filename, "Customers")) then
        dim js as c = a5Helper_generateFileDownloadJS(e.tmpl.alias,filename,"Northwind_Customers.xlsx")
        return js
        trace.writeLn("An error occurred: " + cn.CallResult,"GenExcelFile")
    end if

    return ""

end function

See a5Helper_generateFileDownloadJS() for more info.

Allowed File Extensions 

In web and mobile applications, files can only be downloaded from the Application Server if the file extension is allowed. You can specify the list of allowed file extensions in Project Properties. If a file extension is not listed, Alpha Anywhere will not let you download the file.


Downloading a File Created in an Ajax Callback

In UX and Grid components you might want to make an Ajax callback to the server and then generate a file on the server which the user can then download. The file that you generate on the server might be an image, Excel spreadsheet, PDF, Word document, or other file type.

In this video we show how you can create the Xbasic function that handles the Ajax callback, creates the file and then generates the necessary Javascript to send back to the browser in order to download the file.

Download Component


See Also