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") context.response.SendFile(Session.FormatFileDataURL(FileKey))
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
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: context.response.SendFile(filename,.t.,"MyReport.pdf") end if
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 cn.open("::Name::AADemo-Northwind") if (cn.toExcel(sql, filename, "Customers")) then dim js as c = a5Helper_generateFileDownloadJS(e.tmpl.alias,filename,"Northwind_Customers.xlsx") cn.close() return js else trace.writeLn("An error occurred: " + cn.CallResult,"GenExcelFile") cn.close() end if return "" end function
See a5Helper_generateFileDownloadJS() for more info.
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.