• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 336
  • Last Modified:

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
        End Sub

Open in new window

  • 2
  • 2
1 Solution
You forgot your Response.Clear().

Add this before your AppendHeader.
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.
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 :)
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.

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now