a5Helper_generateFileDownloadJS Function
Syntax
C result = a5Helper_generateFileDownloadJS(C componentAlias, C filename [, C clientsideFilename])
Arguments
- componentAliasCharacter
The alias of the component.
- filenameCharacter
The file to download.
- clientsideFilenameCharacter
Default uses the filename of the file on the server. The filename for the downloaded file.
Returns
- resultCharacter
Returns JavaScript to execute in a browser to download the file.
Description
Generates JavaScript to return from an Ajax Callback to download a file to the client browser.
Discussion
The a5Helper_generateFileDownloadJS() function generates JavaScript for downloading a file to the client's browser. This function can be used in Ajax Callbacks to return a file for download.
For example, suppose you would like to add a button the user can click to download an Excel file. The Excel file is generated on the server in an Ajax Callback.
The Xbasic function below shows how this can be done. The function generateExcelFile() generates an Excel file populated with some static JSON:
function generateExcelFile as c (e as p) dim json as c =<<%json% [ {"id": "1", "firstname" : "Fred", "lastname": "Jones"}, {"id": "2", "firstname" : "Tom", "lastname": "Smith"} ] %json% ' Create a temporary file name dim filename as c = context.request.GetRequestTempFileName(".xlsx") ' Write json to temp file a5_json_to_excel(json, filename) dim clientsideFilename as c = "myexcel.xlsx" dim componentAlias as c componentAlias = e.tmpl.alias ' Generate the javascript response to send the file dim js as c js = a5Helper_generateFileDownloadJS(componentAlias, filename, clientsideFilename) ' Return the JavaScript return js end function
Videos
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.
See Also