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…

Please guide me to get out of this issue…
Why not hide the Div in your OKButton’s ClientClick.
For example: