Link to home
Start Free TrialLog in
Avatar of bd9000
bd9000

asked on

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

ASKER CERTIFIED SOLUTION
Avatar of Bane83
Bane83
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of bd9000
bd9000

ASKER

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.
Avatar of bd9000

ASKER

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