【问题标题】:Call to JavaScript function not working after opening email client with attachment打开带有附件的电子邮件客户端后调用 JavaScript 函数不起作用
【发布时间】:2021-05-07 19:52:16
【问题描述】:
//send the email to the client as eml
Response.ClearHeaders();
Response.Clear();
Response.Buffer = true;
Response.ContentType = "message/rfc822";
Response.AddHeader("content-length", bin.Length.ToString());
Response.AddHeader("content-disposition", "attachment; filename=\"email.eml\"");
Response.OutputStream.Write(bin, 0, bin.Length);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();

//This JavaScript call never fires.                       
ClientScript.RegisterStartupScript(GetType(), Guid.NewGuid().ToString(), "closeLoading();", true);

当我删除上面向用户发送 .eml 文件以打开他们的邮件客户端的代码时,它工作正常。我基本上是在过程完成后尝试关闭加载对话框。这是一个在 C# 中使用 Web 表单的旧应用程序。

我用一个简单的 JavaScript 调用“loading.show();”打开加载弹出窗口

到目前为止,我已经收集到 ContentType 可能把事情搞砸了。有什么想法吗?

谢谢。

【问题讨论】:

  • 检查我的答案。

标签: javascript c# asp.net


【解决方案1】:

Javascript 方法永远不会触发它是因为Response.Flush(); 实际上结束了响应生命周期。

您可以做的是创建一个新的ashx 文件并在那里进行发送电子邮件操作。

假设DownloadMailHandler.ashx 文件:

public class DownloadMailHandler : IHttpHandler  
{  
    public void ProcessRequest(HttpContext context)  
    {  
        var bin = null;//create email content
        context.Response.ClearHeaders();
        context.Response.Clear();
        context.Response.Buffer = true;
        context.Response.ContentType = "message/rfc822";
        context.Response.AddHeader("content-length", bin.Length.ToString());
        context.Response.AddHeader("content-disposition", "attachment; filename=\"email.eml\"");
        context.Response.OutputStream.Write(bin, 0, bin.Length);
        context.Response.Flush();
    }  
  
    public bool IsReusable  
    {  
        get  
        {  
            return false;  
        }  
    }  
}

然后调用Javascript函数并重定向到ashx下载邮件如下:

ClientScript.RegisterStartupScript(this.GetType(), Guid.NewGuid().ToString(), "closeLoading()", true);
Response.Redirect("DownloadMailHandler.ashx");

注意:您没有提到如何获得 bin 值,但您可以将查询字符串传递给 ashx 并创建 bin 对象。见:Passing parameter from .aspx.cs to .ashx

【讨论】:

  • 感谢您的回复。从技术上讲,您提供的内容有效,但并没有解决我遇到的问题。即使在此之后,JavaScript 对话框仍然存在。截至目前,我只是禁用了表单,因此无法重新提交。再次感谢您的宝贵时间,它确实帮助我了解了一些有关通用处理程序的知识。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
  • 2014-08-14
  • 2011-07-01
  • 1970-01-01
  • 2015-01-01
相关资源
最近更新 更多