Subject: How do I let a user download a text file?
Posted By: BrianWren Post Date: 6/29/2005 3:08:06 PM
In processing data specified on a page that allows the user to filter results from a database I write a tab-delimited text file of the data to a specific location on the server.
Following that, I show the data in a table to the user.
If they like what they see, I would like them to be able to download the file that was created.

I have a working app of this in VB6 / IIS, but now I want to do the same thing with VB.NET.

The code in the VB6 WebClass app was (this is a “Custom Web Item” in the WebClass designer):
Private Sub Send_File_Respond()

   On Error GoTo E

   Dim fSize As String:        fSize = GetFileSize("\\the_server\the_path\file.ext")
   Dim oStrm As ADODB.Stream:  Set oStrm = New ADODB.Stream

   Response.Buffer = True      ' Turning on buffering and clearing ensures that 
   Response.Clear              ' all of the header information will be at the
                               ' top.  Feature of IIS 4 and greater.
   oStrm.Open
   oStrm.Type = adTypeBinary
   oStrm.LoadFromFile "\\the_server\the_path\file.ext"

   Response.AddHeader "Content-Disposition", "attachment; filename=file.ext"
   Response.AddHeader "Content-Length", fSize

   ' The fol. value is found in registry at: HKEY_CLASSES_ROOT\MIME\Database\Content Type
   Response.ContentType = "text/plain"
   Response.Charset = "UTF-8"

   Response.BinaryWrite oStrm.Read
   Response.Flush

   oStrm.Close
   Set oStrm = Nothing

R: Exit Sub

E: Dim eN As Long:     eN = Err.Number
   Dim eD As String:   eD = Err.Description
   SetCustomMessages Session, , , _
                     "Error " & eN & "<br>" & _
                     eD & "<br>" & _
                     "in Send_File_Respond()"
   Set NextItem = z__ErrorReport
   Resume R
    
End Sub
How do I duplicate this behavior (puts a "Save" / "Open" dialog on top of Internet Explorer) but without the ADODB features, utilizing the resources of .NET?
Reply By: DARSIN Reply Date: 7/1/2005 4:48:26 AM
http://objectsharp.com/Blogs/bruce/articles/1571.aspx

Reply By: BrianWren Reply Date: 7/1/2005 12:19:57 PM
Thanks.  That was helpful.  
Eventually the link you posted led me to http://support.microsoft.com/kb/307603/EN-US/,How To Write Binary Files to the Browser Using ASP.NET and Visual Basic .NET.”  (Notice that the word “Download” is not to be found in the title;  the keywords for the article were “kbhowtomaster”—that’s right: just the one.  I never would have found this article...)

Go to topic 32359

Return to index page 518
Return to index page 517
Return to index page 516
Return to index page 515
Return to index page 514
Return to index page 513
Return to index page 512
Return to index page 511
Return to index page 510
Return to index page 509