2012-11-22 45 views
0

在我的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参考我的形象也...

enter image description here

请指引我走出这个问题...

回答

1

为什么不在你的OKButton的ClientClick中隐藏Div。

例如:

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

:我必须做的OK按钮单击这两个工作。第一个是隐藏div,还需要下载文件...我可以在OK按钮上同时使用onclick和OnClientClick – Saravanan

1

想想这里发生了什么。

  • 当您点击确定按钮,一些JavaScript正在运行,导致回发到服务器。
  • 在您的OKButton处理程序中,您正在注册一个启动脚本。 这个脚本在这一点上不运行!一旦响应流返回给客户端,它就会运行。
  • 您从OK处理程序执行下载文件方法。
  • 在下载方法中,清除响应,然后执行附件类型响应。

因此,您的RegisterStartupScript未执行!尽可能分出您的客户端和服务器逻辑。

正如Blachshma所说,你可以通过OnClientClick来实现这一点。

1

尝试修改代码:

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