Invoke script to download page to client's machine

     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?
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

When u click a button on your page

write your table again but as a csv


Response.AddHeader("Content-Type", "zip")
Response.AddHeader("Content-Disposition", "attachment; filename=myfile.csv")

this will then prompt them to save myfile.csv


We have a similar functionality where in we show some sales data and later allow client to download it .... you can look at the site go into the foreclosure free trial ...its a blue link on the left hand site .... once u are in just enter the trustee report section and click View Entire report.... after that you would see a report and also a functionality to save the report to xls.

let me know if you want to do something like that .

ZvonkoSystems architectCommented:
My approach would go in another direction.
When you already show the <table> formated data, why not display the table directly in Excel embedded view without a file name and let the user save those view as he likes?

Put at the top of your page before any output this statement:


That will show your <table> in Excel.


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

webkikoAuthor Commented:
lengreen: Because the original data table can take a long time to populate, I don't want to rewrite the table (as a csv or any other format). I want to capture the data that's already displayed in the browser window.

rohanbairat3: I'd already tried your method, but that opens a whole new can of worms. This creates a file on the server which now needs to be maintained (and is a possible security concern?). But more significantly, this creates a headache with multiple simulataneous users. And finally, it's "too complicated" for my users. It requires the user to right-click on a link and use the context-sensitive menu and then select the correct option to save the "target." I really want something that's much easier for the non-tech-savvy to use.

Zvonko and justinbillig: Hmm. I may have to settle for this workaround. I really want to maintain the original page in an HTML table in a browser rather than an embedded Excel spreadsheet. If I do this, doesn't Excel run on the client side? If so, then I have to require *all* my users to have Excel installed in order to see any data. That could be a problem; I'll have to check up on that.

I'm going to keep this question open to see if a better answer comes in. I'm stuck in the mindset of an application developer: I want the user to click a button that invokes a function. Please tell me this is possible.

Thanks for all your suggestions,

U are right about the stuff ... but its been working fine for us since past 3 yrs :) and yes ur right its a headache .... we have auto cleanupscripts written so we dont worry about the cleanup and the creation of file is based on a sessionid and userid base combo ..... but you are right its too complicated and there should be a simpler way. I dont see any security concerns beacause of this though....also u can create a simple button to open the excell file ... u dont need to create a link which needs to be right clicked ....

why not have a page that has two buttons on it

< Download Excel > that button will open a window that will use the script in the link that i gave you to open the file download dialog.

<View Excel In Browser> that button will open a new window that use zvonko's suggestion of writing the Excel out with HTML and changing the content type

<!-- Note if you accept this post, please split the points between me and zvonko' because this post didn't actually do anything -->
webkikoAuthor Commented:
Nobody was able to answer my original question. I wonder if what I want to do is even possible. I still believe it must be; I just don't know how. Thanks for the reasonable alternative solutions. For now, we're gonna go with the Excel object embedded directly in the browser and allow the user to select "File Save As..."
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.