Response.WriteFile outputs the webpage source instead of the text file

"Response.WriteFile outputs the webpage source instead of the text file"

I have a routine that I figured would work correctly, but it is showing a bizzare behavior.
It basically sends a text file (or other type) to the user's browser to initiate a Save As dialog, but instead of the browser displaying either the SaveAs dialog or the text file, it displays the source code of the webpage.
I can't figure this one out as the path is 100% correct (in fact you can copy the path to a browsers URI box and it displays fine).

I'm stumped on this one.  The path is "E:/SITES/SWG/Web/em/Campaign10011.txt" and the file is fine - no security issues as far as I can tell.  
The webroot is "Web" (but the files will ultimately be coming from a non-web root area of the hard drive for security), though I am not using MapPath as I already know the absolute path.
Private Sub DownloadFile(ByVal filepath As String, ByVal forceDownload As Boolean)
            Dim name As String = Path.GetFileName(filepath)
            Dim ext As String = Path.GetExtension(filepath)
            Dim type As String = ""
            ' set known types based on file extension 
            If ext IsNot Nothing Then
                Select Case ext.ToLower()
                    Case ".htm", ".html"
                        type = "text/HTML"
                    Case ".txt"
                        type = "text/plain"
                    Case ".doc", ".rtf"
                        type = "Application/msword"
                End Select
            End If
 
'TEST area to confirm path is correct
'Me.txtPreview.Text = filepath + vbCrLf + "name: " + name + vbCrLf + "ext: " + ext + vbCrLf + "type: " + type + vbCrLf
 
            If forceDownload Then
                Response.AppendHeader("content-disposition", "attachment; filename=" + name)
            End If
            If type <> "" Then
                Response.ContentType = type
            End If
            Response.WriteFile(filepath)
            Response.End()
        End Sub

Open in new window

bd9000Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bane83Commented:
You forgot your Response.Clear().

Add this before your AppendHeader.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bd9000Author Commented:
Ok, I tried that and now it is prompting to save.
The only problem is that the contents of the file is the contents of the web page.
Strange, indeed!
Any ideas what I am doing.
0
bd9000Author Commented:
Ok, I figured it out.
I had a streamwriter that was still open when I called the sub to push the data to the client.  
That quirk is interesting.  I think there may be a way to exploit it in the future :)
Thanks!
0
Bane83Commented:
Oh there are definitely some neat things you can do with that.  I've done things such as emailing page contents to users after they've filled out a questionaire and such.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.