程序控制熄灭和下载的Excel /字功能后会丢失 [英] Program control goes out and lost after downloading excel/word functionality

查看:148
本文介绍了程序控制熄灭和下载的Excel /字功能后会丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这将被用来下载生成word文档C#程序。此外,它生成后,在Hour Glass酒店的UI属性必须被重置为默认值。对于我呼叫一个使用ScriptManager的JavaScript函数,它只能如果语句出现在最后执行。不幸的是,一个步骤之前有一个code下载word文档。当它发生时,程序丢失和从未返回并执行,其中,脚本管理器是present停止沙漏的程序的其余部分。把脚本经理提前下载code也不会帮助。我挨了很多这种奇特的问题。请帮帮忙,谢谢!!

下面是code,

 私人无效DownloadFile(字符串文件名)
{
        System.IO.FileInfo文件=新System.IO.FileInfo(文件名);
        如果(file.Exists)
        {
            HttpContext.Current.Response.AddHeader(内容处置,附件;文件名=+ file.FullName);
            HttpContext.Current.Response.AddHeader(内容长度,file.Length.ToString());
            HttpContext.Current.Response.ContentType =应用程序/ msword
            HttpContext.Current.Response.WriteFile(file.FullName);
            //HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
            ScriptManager.RegisterClientScriptBlock(this.Page,this.GetType(),脚本,stopHourglass(),真正的);
        }
}


解决方案

到Response.End()被调用时,您将不再得到响应从服务器返回因为到Response.End()抛出大自然异常,你可以做一无所知。你需要做的是切换的顺序。一旦你有你的文件可以下载,使用的ScriptManager 来注册一个脚本,它 __ doPostBack 与一个特定的参数。后来在的Page_Load 你会读到这样的说法,并相应地执行下载。同时,经过 ScriptManager.RegisterStartupScript (或 ScriptManager.RegisterClientScriptBlock ),做你清理的逻辑。

在code将如下所示:

  ScriptManager.RegisterClientScriptBlock(this.Page,this.GetType(),DownloadFileKey,__doPostBack('','+ YourArgumentConstStr +');,真正的);
//你这里清理code。

和在Page_Load中:

 如果(请求[__ EVENTARGUMENT] = NULL&放大器;!&安培;请求[__ EVENTARGUMENT] == YourArgumentConstStr)
   //此处下载该文件。

现在你的下一个问题是:是那个最巧妙的可能的方式做到这一点?基本上,如果你坚持具有相同页面上的所有然后是你要实现这样的解决方法。但是,总的趋势是打开另一个选项卡(新页),只有做下载。一种流行的例子就是微软的网站。检查你如何下载.NET框架这里

I have a C# program which will be used to download a generated word document. Also, after generating it, the 'Hour Glass' property in UI has to be reset to default. For that I am calling one javascript function using scriptmanager, which can be executed only if the statement appears at the last. Unfortunately, a step before there is a code to download the word document. when it happens, the program gets lost and never goes back and execute the remaining part of the program in which the script manager is present to stop the hourglass. Putting script manager ahead of the downloading code also wont help. I suffer a lot from this peculiar problem. Please help, Thank you!!

Below is the code.,

private void DownloadFile(string filename)
{
        System.IO.FileInfo file = new System.IO.FileInfo(filename);
        if (file.Exists)
        {
            HttpContext.Current.Response.AddHeader("Content-Disposition", "attachment; filename=" + file.FullName);
            HttpContext.Current.Response.AddHeader("Content-Length", file.Length.ToString());
            HttpContext.Current.Response.ContentType = "Application/msword";
            HttpContext.Current.Response.WriteFile(file.FullName);
            //HttpContext.Current.Response.Flush();
            HttpContext.Current.Response.End();
            ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "script", "stopHourglass()", true);
        }
}

解决方案

When Response.End() is called, you will no longer get a response back from the server because Response.End() throws an exception by nature and you can do nothing about it. What you need to do is switch the order. Once you have your file ready to download, use ScriptManager to register a script that does __doPostBack with a particular argument. Later in Page_Load you will read that argument and execute the download accordingly. Meanwhile, after ScriptManager.RegisterStartupScript (or ScriptManager.RegisterClientScriptBlock), Do your clean up logic.

The Code will look like the following:

ScriptManager.RegisterClientScriptBlock(this.Page, this.GetType(), "DownloadFileKey", "__doPostBack('', '" + YourArgumentConstStr + "');", true);
// You clean up code here.

And in Page_Load:

if (Request["__EVENTARGUMENT"] != null && Request["__EVENTARGUMENT"] == YourArgumentConstStr)
   // Download the file here.

Now your next question will be: Is that the 'neatest' possible way to do it? Basically, if you insist on having everything on the same page then yes you'll have to implement such a workaround. However, the general trend is to open up another tab (new page) that only does the download. One popular example is Microsoft website. Check how you download .NET Framework here.

这篇关于程序控制熄灭和下载的Excel /字功能后会丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆