Link to home
Start Free TrialLog in
Avatar of webkiko
webkiko

asked on

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

<HTML>
<head>
</head>
<BODY >

<% …script to connect to database… %>

<TABLE>
      <TR>
            <TD  <% …script to read data from database… %> </TD>
      </TR>
</TABLE>

<!-- My Script Location -->

</BODY>
</HTML>



      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"
      Response.write(pageStr)
      Response.end
%>

      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?
TIA,      
Avatar of lengreen
lengreen

When u click a button on your page

write your table again but as a csv

use

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

this will then prompt them to save myfile.csv

cheers

Len
SOLUTION
Avatar of rohanbairat3
rohanbairat3

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Avatar of Zvonko
Zvonko
Flag of North Macedonia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of webkiko

ASKER

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

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

-rohan
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 -->
Avatar of webkiko

ASKER

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