Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Invoke script to download page to client's machine

Posted on 2004-11-02
8
Medium Priority
?
262 Views
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 %>

<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,      
0
Comment
Question by:webkiko
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 10

Expert Comment

by:lengreen
ID: 12474948
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
0
 
LVL 7

Assisted Solution

by:rohanbairat3
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 .

rohan
0
 
LVL 63

Accepted Solution

by:
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:

<%
Response.contentyype="application/vnd.ms-excel"
%>


That will show your <table> in Excel.

0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
LVL 15

Expert Comment

by:justinbillig
ID: 12475943
0
 

Author Comment

by:webkiko
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,
Jon
0
 
LVL 7

Expert Comment

by:rohanbairat3
ID: 12477461
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
0
 
LVL 15

Expert Comment

by:justinbillig
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 -->
0
 

Author Comment

by:webkiko
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..."
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

618 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