Text file download from website

G

Guest

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
(e-mail address removed)
 
N

Nick Malik [Microsoft]

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.
 
G

Guest

<%@ 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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top