Invoke script to download page to client's machine

Posted on 2004-11-02
Medium Priority
Last Modified: 2012-05-05
     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?
Question by:webkiko
  • 2
  • 2
  • 2
  • +2
LVL 10

Expert Comment

ID: 12474948
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



Assisted Solution

rohanbairat3 earned 750 total points
ID: 12474976
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 www.realdata.net 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 .

LVL 63

Accepted Solution

Zvonko earned 750 total points
ID: 12475047
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.

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

LVL 15

Expert Comment

ID: 12475943

Author Comment

ID: 12476435
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,

Expert Comment

ID: 12477461

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 ....

LVL 15

Expert Comment

ID: 12487888
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 -->

Author Comment

ID: 12522915
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..."

Featured Post

Take Control of Web Hosting For Your Clients

As a web developer or IT admin, successfully managing multiple client accounts can be challenging. In this webinar we will look at the tools provided by Media Temple and Plesk to make managing your clients’ hosting easier.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

I have helped a lot of people on EE with their coding sources and have enjoyed near about every minute of it. Sometimes it can get a little tedious but it is always a challenge and the one thing that I always say is:   The Exchange of informatio…
There is a wide range of advantages associated with the use of ASP.NET. This is why this programming framework is used to create excellent enterprise-class websites, technologies, and web applications.
SQL Database Recovery Software repairs the MDF & NDF Files, corrupted due to hardware related issues or software related errors. Provides preview of recovered database objects and allows saving in either MSSQL, CSV, HTML or XLS format. Ensures recov…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…

594 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question