0
votes

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.

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

Finally,

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.

my Ok Button event Code is given below:

   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);
    }

downloadFile function is given below:

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 < maxCount && Response.IsClientConnected; i++)
                    {
                        Response.BinaryWrite(_BinaryReader.ReadBytes(1024));
                        Response.Flush();
                    }
                    //if blocks transfered not equals total number of blocks
                    if (i < maxCount)
                        return false;
                    return true;
                }
                catch (Exception ex)
                {
                    return false;
                }
                finally
                {
                   _BinaryReader.Close();
                    myFile.Close();
                }
            }
            else
            {
                return false;
            }

        }//downloadFile ends here...

Here, The particullar file downloaded successfully. But it did not hide the Div Tag Portion.

i tried without invoking the downloadFile function then it will hide the div tag portion successfully.

But i have to first hide the div tag portion and then download the particular file.Kindly refer my image also...

enter image description here

Please guide me to get out of this issue...

3

3 Answers

1
votes

Why not hide the Div in your OKButton's ClientClick.

For example:

<asp:Button ID="OkButton" runat="server" .... OnClick="OkButton_Click"
        OnClientClick="document.getElementById('divName').style.display = 'none';" />
1
votes

Think about what is happening here.

  • When you click OK button, some javascript is running that causes a postback to the server.
  • In your OKButton handler, you are registering a Startup script. This Script does not run at this point! It will run once the response stream returns to the client.
  • You execute the download file method from your OK handler.
  • In the download method, you clear the Response and then perform an attachment type Response.

So, your RegisterStartupScript isn't executing! Seperate out your client and server logic as much as you can.

As Blachshma metions, you can achieve this with OnClientClick.

1
votes

Try to modify your code:

  ClientScript.RegisterStartupScript(GetType(), "popup", "Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(hidePopUp)", true);