1

I am just wanting to drop a pdf file in a folder in my solution and let users download it on my site. Pretty simple!

I have a main .aspx page that contains a static link to another .aspx page that I am using just for downloading a file. The code works if I run the download page directly out of visual studio however if I run my main page and click the that I have pointing to this page it does not work. Here's the code for the download page:

FileInfo file = new FileInfo(Server.MapPath("~/Workflow/Workflow v3.pdf"));            
Response.Clear();
Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "application/pdf";;
Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
Response.BinaryWrite((byte[])File.ReadAllBytes(Server.MapPath("~/Workflow/Workflow v3.pdf")));
Response.Flush();
Response.End();

And just for reference.. here is another download page I am using in a different area of my tool. This page actually takes a parameter and hits the database to grab files stored in a database. This code DOES work, but I don't want to do it this way for my "workflow" download page.

        ...
        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = AgreementDocumentTable.Rows[0]["ContentType"].ToString();
        Response.AppendHeader("Content-Disposition", "attachment; filename=" + AgreementDocumentTable.Rows[0]["Title"].ToString());
        Response.BinaryWrite((byte[])AgreementDocumentTable.Rows[0]["AgreementDocument"]);
        Response.Flush();
        Response.End();
default_noob_network
  • 1,204
  • 3
  • 19
  • 39
  • 1
    You need to be more clear about "does not work". Do breakpoints get hit? is there an exception? etc. – vcsjones Oct 23 '15 at 20:50
  • it breaks at Response.End(); An exception of type 'System.Web.HttpException' occurred in System.Web.dll but was not handled in user code Additional information: The remote host closed the connection. The error code is 0x80070057. – default_noob_network Oct 23 '15 at 20:54
  • I understand Response.End() will throw an exception by design. But there still is no download taking place. – default_noob_network Oct 23 '15 at 20:54

2 Answers2

0

I was able to find this answer that explains you should not use the Response.End() instead it recommends using CompleteRequest() method.

HttpContext throws HttpException

http://blogs.msdn.com/b/aspnetue/archive/2010/05/25/response-end-response-close-and-how-customer-feedback-helps-us-improve-msdn-documentation.aspx

Community
  • 1
  • 1
Mallek
  • 122
  • 5
  • Sorry but this didn't help. I read both of your links thoroughly and attempted a few different things with the information in those posts, but none of it work. I get the same "The remote host closed the connection." error. – default_noob_network Oct 25 '15 at 16:55
0

Got this working by calling a javascript function and using ScriptManager.RegisterClientScriptBlock

This work (don't know 100% why, would like an explanation) so I'm going with it...

Markup:

<a runat="server" id="WorkflowDownloadLink" onserverclick="DownloadWorkflowLink_Click" href="">

Event code:

protected void DownloadWorkflowLink_Click(Object sender, EventArgs e)
    {
        ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Download", "GotoDownloadPage('./Workflow.aspx');", true);
    }

Code behind on Workflow.aspx:

protected void Page_Load(object sender, EventArgs e)
    {

        FileInfo file = new FileInfo(Server.MapPath("~/Workflow/Workflow v3.pdf"));

        Response.Clear();
        Response.Buffer = true;
        Response.Charset = "";
        Response.Cache.SetCacheability(HttpCacheability.NoCache);
        Response.ContentType = "application/octet-stream";
        Response.AddHeader("Content-Disposition", "attachment; filename=" + file.Name);
        Response.BinaryWrite((byte[])File.ReadAllBytes(Server.MapPath("~/Workflow/Workflow v3.pdf")));
        Response.Flush();



    }
default_noob_network
  • 1,204
  • 3
  • 19
  • 39