Invoke script to download page to client's machine
Posted on 2004-11-02
I have an existing (written by someone else) .asp page that reads data from a database and displays it in a table. Below is a simplified view of the .asp code. In reality, many rows of data are read and it can take many minutes to load the page.
<%@ Language=VBScript %>
<% …script to connect to database… %>
<TD <% …script to read data from database… %> </TD>
<!-- My Script Location -->
What I want to do is, after the page is loaded and displayed on the client’s browser, allow the user to save the contents of the page to, say, an Excel spreadsheet on the client’s machine.
I can place the following script where I put the “My Script Location” comment:
Response.ContentType = "application/octet-stream"
Response.AddHeader "content-disposition","attachment; filename=MyFile.xls"
This is almost (but not quite) exactly what I want. This will prompt the user to save the file on the client’s machine (handling all file management functionality for me). If the user enters a valid filename, then an Excel file is written to the client’s disk. But there are two problems: this prevents the page from being displayed on the screen first, and it happens whether or not the user wants to save the file.
What I really want to do is have the page display the entire HTML table and then allow the user (maybe with a button) to take some action to invoke the script that performs the download process.
Any suggestions as to how I can get this done?