C# 如何在asp.net中下载文件之前调用javascript函数?
在我的Asp页面中,我必须在下载Asp页面中的文件之前调用一个javascript函数。我的实际要求是,每当单击下载按钮时,我必须确保下载格式正确 因此,在我的下载按钮事件中,我只显示一个div标记,其中包含两个单选按钮(asp:RadioButton),即docx、pdf和一个OK按钮。选择任何单选按钮后,用户将按OK按钮(asp:button) 最后, 在Ok按钮(asp:Button)事件中,我只是隐藏了div标记部分,根据用户选择的输出格式,文件将被下载 我的“确定”按钮事件代码如下所示:C# 如何在asp.net中下载文件之前调用javascript函数?,c#,javascript,asp.net,html,C#,Javascript,Asp.net,Html,在我的Asp页面中,我必须在下载Asp页面中的文件之前调用一个javascript函数。我的实际要求是,每当单击下载按钮时,我必须确保下载格式正确 因此,在我的下载按钮事件中,我只显示一个div标记,其中包含两个单选按钮(asp:RadioButton),即docx、pdf和一个OK按钮。选择任何单选按钮后,用户将按OK按钮(asp:button) 最后, 在Ok按钮(asp:Button)事件中,我只是隐藏了div标记部分,根据用户选择的输出格式,文件将被下载 我的“确定”按钮事件代码如下所示
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);
}
下载文件功能如下所示:
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...
受保护的bool下载文件(仅限字符串srcURL、字符串docPath、字符串filename)
{
System.IO.FileInfo FileName=新的System.IO.FileInfo(srcURL);
FileStream myFile=newfilestream(srcURL,FileMode.Open,FileAccess.Read,FileShare.ReadWrite);
BinaryReader _BinaryReader=新的BinaryReader(myFile);
if(FileName.Exists)
{
尝试
{
长起始字节=0;
字符串lastUpdateTimeStamp=File.GetLastWriteTimeUtc(docPath.ToString(“r”);
字符串_EncodedData=HttpUtility.UrlEncode(仅文件名,Encoding.UTF8)+lastUpdateTimesTamp;
Response.Clear();
Response.Buffer=false;
AddHeader(“接受范围”、“字节”);
AppendHeader(“ETag”,“\”+\u EncodedData+“\”);
AppendHeader(“上次修改”,lastUpdateItemStamp);
Response.ContentType=“应用程序/八位字节流”;
Response.AddHeader(“内容处置”、“附件;文件名=“+filename.Name”);
AddHeader(“Content-Length”,(FileName.Length-startBytes.ToString());
AddHeader(“连接”,“保持活动”);
Response.ContentEncoding=Encoding.UTF8;
//发送数据
_BaseStream.Seek(startBytes,SeekOrigin.Begin);
//将数据划分为1024字节的包
intmaxcount=(int)Math.天花((FileName.Length-startBytes+0.0)/1024);
//以1024字节块下载
int i;
对于(i=0;i
这里,particullar文件已成功下载。但它没有隐藏Div标记部分
我在没有调用downloadFile函数的情况下进行了尝试,然后它将成功隐藏div标记部分
但我必须先隐藏div标签部分,然后下载特定的文件。请参考我的图片也
请引导我摆脱这个问题……为什么不在OKButton的客户端Click中隐藏Div 例如:
<asp:Button ID="OkButton" runat="server" .... OnClick="OkButton_Click"
OnClientClick="document.getElementById('divName').style.display = 'none';" />
为什么不在OK按钮的ClientClick中隐藏Div呢 例如:
<asp:Button ID="OkButton" runat="server" .... OnClick="OkButton_Click"
OnClientClick="document.getElementById('divName').style.display = 'none';" />
想想这里发生了什么
- 当您单击OK按钮时,一些javascript正在运行,这会导致回发到服务器李>
- 在OKButton处理程序中,您正在注册启动脚本此脚本此时不运行一旦响应流返回到客户端,它将运行李>
- 从OK处理程序执行下载文件方法李>
- 在下载方法中,清除响应,然后执行附件类型响应
作为Blachshma metions,您可以通过OnClientClick实现这一点。想想这里发生了什么
- 当您单击OK按钮时,一些javascript正在运行,这会导致回发到服务器李>
- 在OKButton处理程序中,您正在注册启动脚本此脚本此时不运行一旦响应流返回到客户端,它将运行李>
- 从OK处理程序执行下载文件方法李>
- 在下载方法中,清除响应,然后执行附件类型响应
作为Blachshma metions,您可以通过OnClient单击来实现这一点。尝试修改您的代码:
ClientScript.RegisterStartupScript(GetType(), "popup", "Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(hidePopUp)", true);
尝试修改您的代码:
ClientScript.RegisterStartupScript(GetType(), "popup", "Sys.WebForms.PageRequestManager.getInstance().add_pageLoaded(hidePopUp)", true);
:我必须在单击“确定”按钮时完成这两项工作。第一个是隐藏div,还需要下载文件…我应该同时使用onclick和OnClientClick来点击OK按钮吗:我必须在OK按钮上同时完成这两项工作。第一个是隐藏div,还需要下载文件…我应该在OK按钮上同时使用onclick和OnClientClick吗