By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,362 Members | 3,489 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,362 IT Pros & Developers. It's quick & easy.

Text file download from website

P: n/a
I am trying to download a text file that my .NET page has just created based
on entered parameters on the web page. Everything seems to work and the file
is created. I am using the following code to start the download process:

Response.Clear()
Response.ContentType = "text/plain"
Response.AppendHeader("Content-Disposition", "attachment; filename="
& fileName)
Response.AppendHeader("Content-Description", "This is your Cost
Journal import file.")
Response.Flush()
Response.WriteFile(fileName)
Response.End()

The download does start. However, the "FileName" suggested in the dialoge
to the user is the .NET webpage name without an extention, there is no
default file type, and the description does not display. If a file name is
entered by the user in the "save" the proper information is downloaded. Can
anyone suggest one of two things:
1. What might I be doing incorrectly?
2. Is there a better/simpler way to download a file which has just been
created?
Thank you for your help in advance.
Tom Youngquist
To***@ix.netcom.com
Jul 21 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
why not just use:
response.redirect("myfile.txt")

I'm missing something, I'm sure.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Tom Youngquist" <To***********@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
I am trying to download a text file that my .NET page has just created based on entered parameters on the web page. Everything seems to work and the file is created. I am using the following code to start the download process:

Response.Clear()
Response.ContentType = "text/plain"
Response.AppendHeader("Content-Disposition", "attachment; filename=" & fileName)
Response.AppendHeader("Content-Description", "This is your Cost
Journal import file.")
Response.Flush()
Response.WriteFile(fileName)
Response.End()

The download does start. However, the "FileName" suggested in the dialoge
to the user is the .NET webpage name without an extention, there is no
default file type, and the description does not display. If a file name is entered by the user in the "save" the proper information is downloaded. Can anyone suggest one of two things:
1. What might I be doing incorrectly?
2. Is there a better/simpler way to download a file which has just been
created?
Thank you for your help in advance.
Tom Youngquist
To***@ix.netcom.com

Jul 21 '05 #2

P: n/a
<%@ Page language="VB" Debug="true"%>
<%@ Import Namespace="System" %>
<%@ Import Namespace="System.IO" %>

<script runat="server">
Sub Page_Load(Src As Object, E As EventArgs)
If Not IsPostBack Then
Dim PathVirtual as string = Request.QueryString("Filename")
Dim strPhysicalPath As String
Dim objFileInfo As System.IO.FileInfo
Try
strPhysicalPath = Server.MapPath(PathVirtual)
'exit if file does not exist
If Not System.IO.File.Exists(strPhysicalPath) Then Exit Sub
objFileInfo = New System.IO.FileInfo(strPhysicalPath)
Response.Clear()
'Add Headers to enable dialog display
Response.AddHeader("Content-Disposition", "attachment;
filename=" & Request.QueryString("filename"))
'objFileInfo.Name)
Response.AddHeader("Content-Length",
objFileInfo.Length.ToString())

Response.ContentType = "application/octet-stream"
Response.WriteFile(objFileInfo.FullName)

Catch
'on exception take no action

Finally
'System.IO.File.delete(strPhysicalPath)
'Response.End()

End Try

End If
End Sub
sub funclose(obj as object, e as eventargs)
try
Dim PathVirtual as string = Request.QueryString("Filename")
Dim strPhysicalPath As String
strPhysicalPath = Server.MapPath(PathVirtual)
System.IO.File.delete(strPhysicalPath)
catch
End try
Response.Write("<script language=javascript>" & vbcrlf)
Response.write("self.close();" )
Response.Write("</" & "script>")
End Sub
</script>
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>
<form runat="server">
<table width="100%" border="0"><tr><td align="center"
width="100%"><asp:button id="btnclose" OnClick="funclose" text="Close"
runat="server"/></td></tr></table>
</form>
</body>
</html>
Find Code

"Nick Malik [Microsoft]" wrote:
why not just use:
response.redirect("myfile.txt")

I'm missing something, I'm sure.

--
--- Nick Malik [Microsoft]
MCSD, CFPS, Certified Scrummaster
http://blogs.msdn.com/nickmalik

Disclaimer: Opinions expressed in this forum are my own, and not
representative of my employer.
I do not answer questions on behalf of my employer. I'm just a
programmer helping programmers.
--
"Tom Youngquist" <To***********@discussions.microsoft.com> wrote in message
news:04**********************************@microsof t.com...
I am trying to download a text file that my .NET page has just created

based
on entered parameters on the web page. Everything seems to work and the

file
is created. I am using the following code to start the download process:

Response.Clear()
Response.ContentType = "text/plain"
Response.AppendHeader("Content-Disposition", "attachment;

filename="
& fileName)
Response.AppendHeader("Content-Description", "This is your Cost
Journal import file.")
Response.Flush()
Response.WriteFile(fileName)
Response.End()

The download does start. However, the "FileName" suggested in the dialoge
to the user is the .NET webpage name without an extention, there is no
default file type, and the description does not display. If a file name

is
entered by the user in the "save" the proper information is downloaded.

Can
anyone suggest one of two things:
1. What might I be doing incorrectly?
2. Is there a better/simpler way to download a file which has just been
created?
Thank you for your help in advance.
Tom Youngquist
To***@ix.netcom.com


Jul 21 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.