C# 在客户端上检测Response.BinaryWrite
我有一个带有图像按钮的页面。当用户单击图像按钮时,服务器将创建页面的PDF,并向客户端发送带有下载的响应。问题是这可能需要一些时间。我该如何处理等待客户的问题 我最初的想法是在显示加载程序GIF的按钮上执行onClientClick事件。但我无法检测响应何时完成,以便再次隐藏它。我尝试过使用RegisterStartupScript和Response.Write([script]),但它似乎都被PDF的Response.BinaryWrite(缓冲区)覆盖了—文件被下载了,但没有发生其他任何事情 以下是我的代码,仅包含下载逻辑:C# 在客户端上检测Response.BinaryWrite,c#,asp.net,C#,Asp.net,我有一个带有图像按钮的页面。当用户单击图像按钮时,服务器将创建页面的PDF,并向客户端发送带有下载的响应。问题是这可能需要一些时间。我该如何处理等待客户的问题 我最初的想法是在显示加载程序GIF的按钮上执行onClientClick事件。但我无法检测响应何时完成,以便再次隐藏它。我尝试过使用RegisterStartupScript和Response.Write([script]),但它似乎都被PDF的Response.BinaryWrite(缓冲区)覆盖了—文件被下载了,但没有发生其他任何事情
protected void imgPrint_Click(object sender, ImageClickEventArgs e)
{
PDF pdf = new PDF();
byte[] buffer = pdf.ExportToPDF(Request.QueryString["id"].ToString(), Request.QueryString["mode"].ToString());
string att = "attachment; filename=" + Request.QueryString["mode"].ToString() + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", att);
Response.ContentType = "application/pdf";
Response.ContentEncoding = Encoding.Default;
Response.BinaryWrite(buffer);
}
以及带有onClientClick事件的图像按钮,该事件当前仅显示警报:
<asp:ImageButton ID="imgPrint" runat="server"
ImageUrl="~/bilder/skriv_ut.png" Width="45" Height="36"
onclick="imgPrint_Click" OnClientClick="loader(true);" />
通常,我使用
通用处理程序
和超链接
来下载文件或图片
通用处理程序:
public class DocumentsHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Document doc; // any type
if (doc!= null)
{
context.Response.ContentType = MimeMapping.GetMimeMapping(doc.FileName);
context.Response.AddHeader("Content-Disposition", string.Format("filename={0}", doc.FileName));
context.Response.OutputStream.Write(doc.Data, 0, doc.Data.Length);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
.cs网页:
lnkDocument.NavigateUrl = String.Format("~/Handlers/DocumentsHandler.ashx?{0}",documentId);
当用户想要查看/下载文件时,他们单击超链接。在您的情况下,网页将立即加载,并且在单击超链接后将执行耗时的过程。通常,我使用
通用处理程序和超链接来下载文件或图片
通用处理程序:
public class DocumentsHandler : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
Document doc; // any type
if (doc!= null)
{
context.Response.ContentType = MimeMapping.GetMimeMapping(doc.FileName);
context.Response.AddHeader("Content-Disposition", string.Format("filename={0}", doc.FileName));
context.Response.OutputStream.Write(doc.Data, 0, doc.Data.Length);
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
.cs网页:
lnkDocument.NavigateUrl = String.Format("~/Handlers/DocumentsHandler.ashx?{0}",documentId);
当用户想要查看/下载文件时,他们单击超链接。在您的情况下,web页面将立即加载,并且在单击超链接后将执行耗时的过程。在webform中,您可以使用Jquery&Webservice
function loader(show) {
if(show)
{
//show loading here
}
$.ajax({
type: "POST",
url: "MyService.asmx/myMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//do some thing & hide loading
},
error: function (data) {
//do some thing & hide loading
}
});
}
});
[WebService, ScriptService]
public class MyService : WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string myMethod()
{
PDF pdf = new PDF();
byte[] buffer = pdf.ExportToPDF(Request.QueryString["id"].ToString(), Request.QueryString["mode"].ToString());
string att = "attachment; filename=" + Request.QueryString["mode"].ToString() + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", att);
Response.ContentType = "application/pdf";
Response.ContentEncoding = Encoding.Default;
Response.BinaryWrite(buffer);
return "ok";
}
}
在webform中,您可以使用Jquery和Webservice
function loader(show) {
if(show)
{
//show loading here
}
$.ajax({
type: "POST",
url: "MyService.asmx/myMethod",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function (data) {
//do some thing & hide loading
},
error: function (data) {
//do some thing & hide loading
}
});
}
});
[WebService, ScriptService]
public class MyService : WebService
{
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public static string myMethod()
{
PDF pdf = new PDF();
byte[] buffer = pdf.ExportToPDF(Request.QueryString["id"].ToString(), Request.QueryString["mode"].ToString());
string att = "attachment; filename=" + Request.QueryString["mode"].ToString() + ".pdf";
Response.ClearContent();
Response.AddHeader("content-disposition", att);
Response.ContentType = "application/pdf";
Response.ContentEncoding = Encoding.Default;
Response.BinaryWrite(buffer);
return "ok";
}
}
酷,学到了新东西!但是,我的问题仍然存在——如何检测响应何时传递,以便隐藏加载程序?时间消耗是从点击到文件准备好下载之间。酷,学到了一些新东西!但是,我的问题仍然存在——如何检测响应何时传递,以便隐藏加载程序?时间消耗是从单击到文件准备好下载之间。谢谢。我用PageMethod尝试了类似的方法。但是,我得到了一个关于返回的数据与Json不兼容的异常。我猜你的会出现同样的错误,但我会尝试一下,只是为了确定。谢谢。我用PageMethod尝试了类似的方法。但是,我得到了一个关于返回的数据与Json不兼容的异常。我猜你的会出现同样的错误,但我会尝试一下以确定。