tags:

views:

400

answers:

3

Hello,

I was wondering what's the best practise for serving a generated big file in classic asp.

We have an application with "export to excel" function that produces 10MB files. The excels are created by just calling a .asp page that has the Response.ContentType set to excel and has an HTML table for the data.

This gives as problem that it takes 4 minutes before the user sees the "Save as..." dialog.

My current solution is to call an .asp page that creates the excel on the server with AJAX and lets the page return the URL of the generated document. Then I can use javascript to display the on the original page.

Is this easy to do with classic asp (creating files on server with some kind of stream) while keeping security in mind? (URL should make people be able to guess the location of other files)

How would I go about handling deleted the generated files overtime? They have to be deleted periodicly as the data changes in realtime.

Thanks.

edit: I realized now that creating the file on the server will probably also take 4 minutes...

A: 

How do you plan to generate the Excel? I hope you don't plan to call Excel to do that, as it is unsupported, and generally won't work well.

You should check to see if there are COM components to generate Excel that you can call from Classic ASP. Alternatively, add one ASP.NET page for the purpose. I know for a fact that there are compoonents that can be called from ASP.NET pages to do this. Worse come to worst, there's an Excel exporter component from Infragistics that works with their UltraWebGrid control to export. The grid need not be visible in order to accomplish this, but styles in the grid translate to styles in the spreadsheet. They also allow you to manipulate the spreadsheet programmatically.

John Saunders
John: is really easy to generate an Open format XML excel file using ASP classic (or anything for the matter). I think you are complicating the thing a little bit.
Eduardo Molteni
Note that I asked how he was going to create the Excel. I could not assume he was going to generate it as XML, especially since I've seen people try to do this by calling Excel through COM!
John Saunders
I'm currently doing it trough a HTML table and set response.contenttype on excel.
Thomas Stock
+2  A: 

I think you are selecting a complex route, when the solution is simple enough (Though I may be missing some requirements)

If you to generate an excel, just call an asp page that do the following:

Response.clear
Response.AddHeader "content-disposition", "attachment; filename=myexcel.xls"
Response.ContentType = "application/excel"

'//write the content of the file
Response.write "...."

Response.end

This will a start a download process in the browser without needing to generate a extra call, javascript or anything

See this question for more info on the format you will choose to generate the excel.

Edit

Since Thomas update the question and the real problem is that the file take 4 minutes to generate, the solution could be:

  1. Offer the user the send the file by email (if this is a workable solution in you server or hosting).

  2. Generate the file async, and let the user know when the file generation is done (with an ajax call, like SO does when other user have added an answer)

To generate the file on the server

'//You should change for a random name or something that makes sense
FileName = "C:\temp\myexcel.xls" 
FileNumber = FreeFile
Open FileName For Append As #FileNumber

'//generate the content
TheRow = "...."
Print #FileNumber, TheRow




Close #FileNumber

To delete the temp files generated
I use Empty Temp Folders a freeware app that I run daily on the server to take care of temp files generated. (Again, it depends on you server or hosting)

About security
Generate the files using random numbers or GUIds for a light protection. If the data is sensitive, you will need to download the file from a ASP page, but I think that you will be in the same problem again...(waiting 4 minutes to download)

Eduardo Molteni
That's how we currently do it, but it takes way too long before the user sees the "save as..." dialog popping up..The server has to buffer 4500 <tr>'s before it can offer the excel download to the user. This takes quite some time.
Thomas Stock
But you're probably right that that speed will not be increased by first generating the file on the server.. I didn't really think about that.
Thomas Stock
Thomas: what about using Response.flush from time to time while you generate the file. Does it make some difference? (I have used it in the past, but never test if it make any difference)
Eduardo Molteni
Is that something such as the response buffer? Using that causes the data to display in a regular browser window instead of being offered in a save as dialog..
Thomas Stock
Sorry, you are right. I understand your problem now, I will edit the answer.
Eduardo Molteni
Lots of info, thanks! Will suggest this to my project manager.I think I'll first check the # of records with a query, and when it's higher than 1000 I can offer to email it or do ajax polling. (should be no problem on the server).
Thomas Stock
+1 For the general idea of async notification. However please note that VBScript does not have an open statement. Scripting.FileSystemObject or ADODB.Stream are the tools needed to create files server-side.
AnthonyWJones
A: 
  1. Read file using FSO.
  2. Set headers for Excel file-type, name according to file read and for download (attachment)
  3. Flush response after headers are set. The client should display "save as" dialogue.
  4. Output FSO to response. Client will download file and see progress bar.
Espen