title: Sending File to browser / response.write alternative
body: <p>On my aspx page I have a dynamically created menu of links to documents that are stored in an Oracle Database.</p>
  <p>When the user clicks on a link within the menu, it calls a JScript function which calls a WebMethod decorated vb.net Procedure in the code behind page.  This function is passed the element ID which it uses to query the Oracle database and return the corresponding blob object which is the document the user requested.</p>
  <p>When I was testing this, I hooked it up to a button, hard coded everything and was able to return the document to the browser which worked with no issues. Below is what I used to return the blob object to the browser.</p>
  <pre><code>Response.Clear()
  Response.ClearContent()
  Response.ClearHeaders()
  
  Response.Buffer = True
  Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + docname)
  Response.ContentType = &quot;application/pdf&quot;
  
  Response.BinaryWrite(bytes)
  </code></pre>
  <p>Now that I've moved the code into the callable method.  I just learned that I can't use Response to write the file i've pulled from the database to the browser.  Or if it can be done I have no clue how to do it.  Here is the following code.</p>
  <blockquote>
  <p>ASPX page.</p>
  </blockquote>
  <pre><code> &lt;%@ Page Title=&quot;&quot; Language=&quot;vb&quot; AutoEventWireup=&quot;false&quot; MasterPageFile=&quot;~/Site.Master&quot; CodeBehind=&quot;FrmBookMain.aspx.vb&quot; Inherits=&quot;ccc.com.FormsBook&quot; %&gt;
  
  &lt;%@ Register Assembly=&quot;Obout.Ajax.UI&quot; Namespace=&quot;Obout.Ajax.UI.TreeView&quot; TagPrefix=&quot;obout&quot; %&gt;
  &lt;%@ Register assembly=&quot;obout_Splitter2_Net&quot; namespace=&quot;OboutInc.Splitter2&quot; tagprefix=&quot;obspl&quot; %&gt;
  &lt;%@ Register assembly=&quot;System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&quot; namespace=&quot;System.Web.UI&quot; tagprefix=&quot;cc1&quot; %&gt;
  &lt;%@ Register assembly=&quot;obout_Interface&quot; namespace=&quot;Obout.Interface&quot; tagprefix=&quot;cc2&quot; %&gt;
  &lt;%@ Register assembly=&quot;obout_EasyMenu_Pro&quot; namespace=&quot;OboutInc.EasyMenu_Pro&quot; tagprefix=&quot;oem&quot; %&gt;
  &lt;%@ Register assembly=&quot;System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a&quot; namespace=&quot;System.Web.UI.HtmlControls&quot; tagprefix=&quot;cc3&quot; %&gt;
  
  
  &lt;asp:Content ID=&quot;Content1&quot; ContentPlaceHolderID=&quot;HeadContent&quot; runat=&quot;server&quot;&gt;
  &lt;/asp:Content&gt;
  &lt;asp:Content ID=&quot;Content2&quot; ContentPlaceHolderID=&quot;MainContent&quot; runat=&quot;server&quot;&gt;
  
  &lt;div style=&quot;width:900px; height:450px&quot;&gt;
  
  
  &lt;obspl:Splitter ID=&quot;spl1&quot; runat=&quot;server&quot;&gt;
      
      &lt;leftpanel&gt;
          &lt;content&gt;
              &lt;asp:PlaceHolder ID=&quot;placeHolder1&quot; EnableViewState=&quot;false&quot; runat=&quot;server&quot;&gt;&lt;/asp:PlaceHolder&gt;
          &lt;/content&gt;
      &lt;/leftpanel&gt;
     
      &lt;RightPanel&gt;
          &lt;Content&gt;
              &lt;div class=&quot;tdText&quot; style=&quot;width:400px;height:80%;padding-left:30px;padding-top:30px&quot;&gt;
                          &lt;h2&gt;Document Repository&lt;/h2&gt;
                          Choose a document from the left menu...
              &lt;/div&gt;
          &lt;/Content&gt;
      &lt;/RightPanel&gt;
  &lt;/obspl:Splitter&gt;
  
  &lt;/div&gt;
  &lt;asp:ScriptManager ID=&quot;ScriptManager1&quot; runat=&quot;server&quot; EnablePageMethods=&quot;true&quot;/&gt;
  &lt;script type=&quot;text/javascript&quot; src=&quot;../Java/Frm_JScript.js&quot;/&gt; 
  
  &lt;/asp:Content&gt;
  </code></pre>
  <blockquote>
  <p>JavaScript Code</p>
  </blockquote>
  <pre><code>function GetDocument(src) {
   PageMethods.wbmGetDocument(src);
  }
  </code></pre>
  <blockquote>
  <p>VB.net Module</p>
  </blockquote>
  <pre><code>Module modGetDoc_Frm
  
  &lt;WebMethod()&gt;
      Public Sub wbmGetDocument(ByVal sID As String)
  
          Dim dr As OracleDataReader = Get_DataReader(sSql_GetDocument(sID))
          Dim blob As OracleBlob = dr.GetOracleBlob(1)
  
          Dim bytes(blob.Length) As Byte : blob.Read(bytes, 0, blob.Length)
  
          Dim doctype As String = dr(2).ToString()
          Dim docname As String = dr(3).ToString()
          
        *** Everything below won't work in module is there an alternative or what am I doing wrong***
          Response.Clear()
          Response.ClearContent()
          Response.ClearHeaders()
          Response.Buffer = True
          
          Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + docname)
          Response.ContentType = &quot;application/pdf&quot;
          Response.BinaryWrite(bytes)
  
  
      End Sub
  
  
  End Module
  </code></pre>
  <h2>FIX</h2>
  <p>As @Icarus said below I added a .ashx page called it from the java script, and allowed it to process the files.  Doing it this way allows you to access response stream from codebehind.
  This link helped me understand .ashx pages in more detail
  <a href="http://www.dotnetperls.com/ashx" rel="nofollow noreferrer">http://www.dotnetperls.com/ashx</a></p>
  <blockquote>
  <blockquote>
  <p>JavaScript</p>
  </blockquote>
  </blockquote>
  <pre><code>function GetDocument(id) {
      window.open('FrmDocHandler.ashx?ID=' + id);
  }
  </code></pre>
  <blockquote>
  <blockquote>
  <p>.ashx code</p>
  </blockquote>
  </blockquote>
  <pre><code> Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
  
          Dim sID As String = context.Request.QueryString(&quot;id&quot;)
  
          Dim fileName As String
          Dim fileType As String
          Dim bytes() As Byte
  
          bytes = Get_Blob(fileName, fileType, sSql_GetDocument(sID))
          context.Response.Clear()
          'clear the content of the browser
          context.Response.ClearContent()
          context.Response.ClearHeaders()
          context.Response.Buffer = True
  
          context.Response.AddHeader(&quot;Content-Disposition&quot;, &quot;attachment; filename=&quot; + fileName)
  
          context.Response.ContentType = GetMIMEType(fileType)
  
          context.Response.BinaryWrite(bytes)
  </code></pre>