title: how to invoke a javascript function just before downloding a file in asp.net?
body: <p>In my Asp page, i have to invoke one javascript function just before download a file in asp page. My actual requirement is i have to make sure that the downloading format whenever click the download button.</p>
  
  <p>So, In my download button event i just show a div tag that contains two radio buttons(asp:RadioButton) namely docx,pdf and also one OK button.after selecting any radio button, the user will press ok Button(asp:Button).</p>
  
  <p>Finally,</p>
  
  <p>In the Ok Button(asp:Button) event i just hide the div tag portion and the based on the selected output format user selected,the file will be downloading.</p>
  
  <p>my Ok Button event Code is given below:</p>
  
  <pre><code>   protected void OKButton(object sender, EventArgs e)
      {
          string outputType = "";
          if (docx.Checked == true)
          {
              outputType = "docx";
          }
          else
          {
              outputType = "pdf";
          }
  
          filename=filename+"."+outputType;
         ClientScript.RegisterStartupScript(GetType(), "popup", "hidePopUp()", true);
         bool isDownloaded=downloadFile(strURL, docPath, filename);
      }
  </code></pre>
  
  <p>downloadFile function is given below: </p>
  
  <pre><code>protected bool downloadFile(string srcURL, string docPath, string filenameOnly)
          {
              System.IO.FileInfo FileName = new System.IO.FileInfo(srcURL);
              FileStream myFile = new FileStream(srcURL, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);
              BinaryReader _BinaryReader = new BinaryReader(myFile);
  
              if (FileName.Exists)
              {
                  try
                  {
                      long startBytes = 0;
                      string lastUpdateTiemStamp = File.GetLastWriteTimeUtc(docPath).ToString("r");
                      string _EncodedData = HttpUtility.UrlEncode(filenameOnly, Encoding.UTF8) + lastUpdateTiemStamp;
  
                      Response.Clear();
                      Response.Buffer = false;
                      Response.AddHeader("Accept-Ranges", "bytes");
                      Response.AppendHeader("ETag", "\"" + _EncodedData + "\"");
                      Response.AppendHeader("Last-Modified", lastUpdateTiemStamp);
                      Response.ContentType = "application/octet-stream";
                      Response.AddHeader("Content-Disposition", "attachment;filename=" + FileName.Name);
                      Response.AddHeader("Content-Length", (FileName.Length - startBytes).ToString());
                      Response.AddHeader("Connection", "Keep-Alive");
                      Response.ContentEncoding = Encoding.UTF8;
  
                      //Send data
                      _BinaryReader.BaseStream.Seek(startBytes, SeekOrigin.Begin);
  
                      //Dividing the data in 1024 bytes package
                      int maxCount = (int)Math.Ceiling((FileName.Length - startBytes + 0.0) / 1024);
  
                      //Download in block of 1024 bytes
                      int i;
                      for (i = 0; i &lt; maxCount &amp;&amp; Response.IsClientConnected; i++)
                      {
                          Response.BinaryWrite(_BinaryReader.ReadBytes(1024));
                          Response.Flush();
                      }
                      //if blocks transfered not equals total number of blocks
                      if (i &lt; maxCount)
                          return false;
                      return true;
                  }
                  catch (Exception ex)
                  {
                      return false;
                  }
                  finally
                  {
                     _BinaryReader.Close();
                      myFile.Close();
                  }
              }
              else
              {
                  return false;
              }
  
          }//downloadFile ends here...
  </code></pre>
  
  <p>Here, The particullar file downloaded successfully. But it did not hide the Div Tag Portion.</p>
  
  <p>i tried without invoking the downloadFile function then it will hide the  div tag portion successfully.</p>
  
  <p>But i have to first hide the div tag portion and then download the particular file.Kindly refer my image also...</p>
  
  <p><img src="https://i.stack.imgur.com/Xgk10.png" alt="enter image description here"></p>
  
  <p>Please guide me to get out of this issue...</p>