在我的Asp页面中,我必须在asp页面下载文件之前调用一个javascript函数。我的实际要求是我必须确保下载格式,每当点击下载按钮。如何在asp.net中下载文件之前调用JavaScript函数?
因此,在我的下载按钮事件中,我只是显示一个包含两个单选按钮(asp:RadioButton)的doc标签,即docx,pdf和一个OK按钮。选择任何单选按钮后,用户将按下OK按钮ASP:按钮)。
最后,
在OK按钮(ASP:按钮)事件我只是隐藏div标签部分和基于所选择的选择的输出格式的用户,该文件将被下载。下面
我的OK按钮事件代码给出:
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函数如下:
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...
在这里,成功下载该文件particullar。但它没有隐藏Div标签部分。
我尝试不调用downloadFile函数,然后它会成功隐藏div标签部分。
但我必须先隐藏div标签部分,然后下载特定file.Kindly参考我的形象也...
请指引我走出这个问题...
:我必须做的OK按钮单击这两个工作。第一个是隐藏div,还需要下载文件...我可以在OK按钮上同时使用onclick和OnClientClick – Saravanan