downloading a file in ASP.NET app

  • Thread starter Thread starter AlexB
  • Start date Start date
A

AlexB

I'm trying to download a text file to an IE client using
the following code. The problem is that the saved file
also includes the html for the page.

Any ideas anyone?


Private Sub Button5_Click(ByVal sender As System.Object,
ByVal e As System.EventArgs) Handles Button5.Click

Dim strFileToDownload$ = "C:\test.log"
Response.ContentType = "application/octet-stream"
Response.AddHeader("Content-
Disposition", "attachment; filename = " &
System.IO.Path.GetFileName(strFileToDownload))
Response.Flush()
Response.WriteFile(strFileToDownload)

End Sub
 
You might try contenttype/text

Alternatively

You can try the following: The link can be placed into
a .aspx page as well. When the user clicks the link he/she
will be prompted to open or save the file.
<html>
<body>
<a href="download.txt">Download File</a>
</body>
</html>
 
Thanks Billy for suggestions.

Hi AlexB,

Here are 3 Members of Response:
Clear Clears all content output from the buffer stream.
Flush Sends all currently buffered output to the client.
End Sends all currently buffered output to the client, stops execution of
the page, and raises the Application_EndRequest event.

So, the code should be like this:
//Response.Flush();
Response.Clear ();
Response.WriteFile(strFileToDownload);
Response.End ();

Hope this helps.


Best Regards,
Lewis

Get Secure! - <www.microsoft.com/security>
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
| Content-Class: urn:content-classes:message
| From: "AlexB" <[email protected]>
| Sender: "AlexB" <[email protected]>
| Subject: downloading a file in ASP.NET app
| Date: Fri, 1 Aug 2003 13:45:24 -0700
| Lines: 19
| Message-ID: <[email protected]>
| MIME-Version: 1.0
| Content-Type: text/plain;
| charset="iso-8859-1"
| Content-Transfer-Encoding: 7bit
| X-Newsreader: Microsoft CDO for Windows 2000
| Thread-Index: AcNYbdUuIaLhfqpbSbWrlNVaajyWHQ==
| X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
| Newsgroups: microsoft.public.dotnet.general
| Path: cpmsftngxa06.phx.gbl
| Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.general:103166
| NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
| X-Tomcat-NG: microsoft.public.dotnet.general
|
| I'm trying to download a text file to an IE client using
| the following code. The problem is that the saved file
| also includes the html for the page.
|
| Any ideas anyone?
|
|
| Private Sub Button5_Click(ByVal sender As System.Object,
| ByVal e As System.EventArgs) Handles Button5.Click
|
| Dim strFileToDownload$ = "C:\test.log"
| Response.ContentType = "application/octet-stream"
| Response.AddHeader("Content-
| Disposition", "attachment; filename = " &
| System.IO.Path.GetFileName(strFileToDownload))
| Response.Flush()
| Response.WriteFile(strFileToDownload)
|
| End Sub
|
 
Back
Top