954,574 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Response.AddHeader .Net 4 not working?

Hi

I'm writing a site that allows users to download documents securely stored on the network off of the web server.

I'm using a com object on the server that we've been using here for ages that fetches the file from the network in a binary stream thus preventing user access to the network.

The problem I'm having is when the user clicks on the link, instead of the file opening, the page reloads itself with the stream as its source.

On the open save dialog instead of filename.* I get MyPage.aspx as the file name it is as if my response.AddHeader is not working.

It all works fine on my localbox -it only happens after I publish to a test deployment server.

(This is the first time I've tried this in Visual studio 2010 with .Net framework 4.)


Here is my Page and code:

<!-- inside of a datarepeater....-->

<asp:LinkButton ID="MyLink" runat="server" CommandArgument='<%# Eval("FilePath") %>' CommandName='<%# Eval("FileName") %>' ToolTip="Open File" OnCommand="LinkButton1_Click" CssClass="PrimaryNavigation"><%# Eval("FileName") %></asp:LinkButton>

And here is the code behind:

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        Dim o As Object = CreateObject("#######") 'object that brings back stream
        Dim vntStream() As Byte
        Dim CmdArg, CmdName As String
        CmdArg = e.CommandArgument 'fullpath to file 
        CmdName = e.CommandName 'Filename
       'get binary stream
        vntStream = o.FetchDocument(Server.UrlDecode(CmdArg.ToString)) 
       
        'Add header - does not seam to be working
        Response.AddHeader("Content-Disposition", "Attachment; Filename=" & CmdName)

        'Write Out Binary stream
        Response.BinaryWrite(vntStream)

        'Finish
        Response.End()

    End Sub


Like I said, it looks like the line Response.AddHeader("Content-Disposition", "Attachment; Filename=" & CmdName) is not working.

Anyone out there see what's going wrong?

G_Waddell
Posting Whiz in Training
255 posts since Nov 2009
Reputation Points: 43
Solved Threads: 27
 

Hi all,
I found it through trial and error.

I added Response.Clear and Response.ClearHeaders before appending my header and it did the trick.

Protected Sub LinkButton1_Click(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.CommandEventArgs)
        Dim o As Object = CreateObject("#######") 'object that brings back stream
        Dim vntStream() As Byte
        Dim CmdArg, CmdName As String
        CmdArg = e.CommandArgument 'fullpath to file 
        CmdName = e.CommandName 'Filename
       'get binary stream
        vntStream = o.FetchDocument(Server.UrlDecode(CmdArg.ToString)) 
        Response.Clear()
        Response.ClearHeaders()
        'Add header - does not seam to be working
        Response.AddHeader("Content-Disposition", "Attachment; Filename=" & CmdName)

        'Write Out Binary stream
        Response.BinaryWrite(vntStream)

        'Finish
        Response.End()

    End Sub
G_Waddell
Posting Whiz in Training
255 posts since Nov 2009
Reputation Points: 43
Solved Threads: 27
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: