views:

118

answers:

2

I have an ASP.NET page where I'm generating an Excel spreadsheet and sending that in the response (content-disposition header). That part works great, but I have a button that never becomes re-enabled because the original page isn't part of the response.

Here's the function I'm using:

Public Shared Sub WriteToResponse(ByVal theWorkBook As Workbook, ByVal FileName As String, ByVal resp As HttpResponse)
    Dim theStream As New System.IO.MemoryStream()
    theWorkBook.Save(theStream)

    Dim byteArr As Byte() = DirectCast(Array.CreateInstance(GetType(Byte), theStream.Length), Byte())

    theStream.Position = 0
    theStream.Read(byteArr, 0, CInt(theStream.Length))
    theStream.Close()

    resp.Clear()
    resp.AddHeader("content-disposition", "attachment; filename=" & FileName)
    resp.BinaryWrite(byteArr)
    resp.End()
End Sub

I tried not clearing the response and using AppendHeader instead of AddHeader, but the response still contains only the file.

What am I missing? Do I need to use a different header? I found this unanswered question on the ASP.NET forums; sounds like my problem.

+2  A: 

You cannot send back a file and a new page in the same response.

I'm not sure how the solution fits your particular needs, but remember that you can use Javascript to enable or disable a button right before the file-creating request is sent to the server (so that, by the time spreadsheet download begins for the user, the button is already in the state you want it in).

lance
+2  A: 

You can't. You can fake the behaviour by opening a popup on clicking the button and have the popup serve up the file and posting back the page from the javascript. But in this manner you are effectively creating 2 requests with 2 responses.

update: if you want to disable the button when they start the download and enable it as soon as the download starts you can't really do this but you could use some trickery to make it appear like that. When they click on the download button use some javascript to disable the button and to create the popup that kicks off the file streaming. When the filestreaming function (WriteToResponse) returns set some value in the users session. In the mean time have the original page poll the server using AJAX to see if the session value is set. If it's set re-enable the button and set to the value to empty or false again (so this works a 2nd time). But this seems like a whole lot of tricks.

but I think in this case the cure is worse then the problem. I assume you want this because generating the file takes a while and you don't want users to reset the flow by pressing the button impatiently while the file is being prepared. To accomplish this I would just show a popup message in the page on button press saying something like "Your download is being prepared. This might take a minute. Thank you for your patience. (click to dismiss this message)"

olle