【问题标题】:how to invoke a javascript function just before downloding a file in asp.net?如何在 asp.net 中下载文件之前调用 javascript 函数?
【发布时间】:2012-11-22 21:18:59
【问题描述】:

在我的 Asp 页面中,我必须在下载 asp 页面中的文件之前调用一个 javascript 函数。我的实际要求是每次单击下载按钮时都必须确保下载格式。

所以,在我的下载按钮事件中,我只显示一个包含两个单选按钮(asp:RadioButton)的 div 标签,即 docx、pdf 和一个 OK 按钮。选择任何单选按钮后,用户将按下 ok 按钮(asp :按钮)。

最后,

在 Ok Button(asp:Button) 事件中,我只是隐藏了 div 标签部分,并且基于用户选择的输出格式,文件将被下载。

我的 Ok Button 事件代码如下:

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

这里,特定文件下载成功。但它并没有隐藏 Div Tag 部分。

我尝试不调用 downloadFile 函数,然后它会成功隐藏 div 标签部分。

但我必须先隐藏 div 标签部分,然后下载特定文件。请参考我的图片...

请指导我摆脱这个问题...

【问题讨论】:

    标签: c# javascript asp.net html


    【解决方案1】:

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

    例如:

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

    【讨论】:

    • :我必须在 Ok Button Click 上完成这两项工作。第一个是隐藏 div 并且还需要下载文件...我应该在 OK 按钮上同时使用 onclick 和 OnClientClick
    【解决方案2】:

    想想这里发生了什么。

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

    所以,您的 RegisterStartupScript 没有执行!尽可能多地分离您的客户端和服务器逻辑。

    正如 Blachshma 提到的,您可以通过 OnClientClick 实现这一点。

    【讨论】:

      【解决方案3】:

      尝试修改你的代码:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2011-08-11
        • 2021-10-07
        • 2014-02-09
        • 2021-05-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多