Search code examples
.netvb.netdownloadsave-as

VB.net file-download prevents postback


I want to generate a (report) file for users to download, then display a message to users to show the process completed successfully.

Dim myFile As FileInfo = New FileInfo(<filepath>)

'My function to generate a report (this can take a few minutes)
GenerateReport(myFile.FullName)

'Prompt user with "save as" dialog box.
Response.Clear()
Response.ClearHeaders()
Response.AddHeader("Content-Disposition", "attachment; filename=" & _
Response.WriteFile()

'Give message to say generation is complete. (This line doesn't take effect)
lblMsg.Text = "Generation complete"

However, when I use create a "save as" box, it seems to stop the message appearing at all. Why is that? And can anyone suggest a way around it?


Solution

  • It seems this is quite a common problem. But for all the suggestions I've come across, few seem to offer any sort of solution. Here's a useful article in Rick Strahl's blog.

    One useful method is MyWebClient.DownloadFile. This downloads the file ok, but doesn't give a "save as" dialogue box as required.