Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / General / January 2005

Tip: Looking for answers? Try searching our database.

Text file download from website

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Youngquist - 27 Jan 2005 23:41 GMT
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
Tom_Y@ix.netcom.com
Nick Malik [Microsoft] - 28 Jan 2005 07:02 GMT
why not just use:
response.redirect("myfile.txt")

I'm missing something, I'm sure.

Signature

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

> 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
[quoted text clipped - 21 lines]
> Tom Youngquist
> Tom_Y@ix.netcom.com
RiteshDotNet - 28 Jan 2005 15:49 GMT
<%@ 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

> why not just use:
> response.redirect("myfile.txt")
[quoted text clipped - 31 lines]
> > Tom Youngquist
> > Tom_Y@ix.netcom.com

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.