Invoke script to download page to client's machine

Posted on 2004-11-02
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
    LVL 10

    Expert Comment

    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


    LVL 7

    Assisted Solution

    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 .

    LVL 63

    Accepted Solution

    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.

    LVL 15

    Expert Comment


    Author Comment

    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,
    LVL 7

    Expert Comment


    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

    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

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

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Suggested Solutions

    I would like to start this tip/trick by saying Thank You, to all who said that this could not be done, as it forced me to make sure that it could be accomplished. :) To start, I want to make sure everyone understands the importance of utilizing p…
    Have you ever needed to get an ASP script to wait for a while? I have, just to let something else happen. Or in my case, to allow other stuff to happen while I was murdering my MySQL database with an update. The Original Issue This was written…
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
    In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

    875 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

    Need Help in Real-Time?

    Connect with top rated Experts

    8 Experts available now in Live!

    Get 1:1 Help Now