I am trying to export a particular string as a csv file and I want the user to be able to download this file. IHP has the createTemporaryDownloadUrl function that takes in a StoredFile and then creates a temporary download link. The problem is, I don't want to store this CSV file on the server since it requires additional overhead, such as deleting the file after the temporary download link has expired.
I am able to render the text variable using renderPlain, but I'm not entirely sure how to automate the downloading process (since the user would have to right click, download as, and then rename the file themselves)
I first tried using the regular createTemporaryDownloadUrl method by storing the file, but there is no easy way of deleting the file after the temporary download url expires. I'm able to display the CSV contents as a plain text file, but there is no obvious way of having the user download it.
In your action, you can set the
Content-Dispositionheader in order to trigger the download of a variable of typeText. In this example, I'll usefileNameandfileContentto be the file's name and content, respectively:setHeaderis used to tell the browser to download a file with the given name, and thenrenderPlainsends the browser the file's content.Of course, this can be used for any type of text file by changing the filename to a different file extension.
Hope this helps!