通过 IHttpAsyncHandler 发送文件时遇到问题

发布于 2024-08-08 16:00:34 字数 1595 浏览 4 评论 0原文

我使用 IHttpHandler 调用 Web 服务并将生成的 byte[] 作为下载的文件附件返回给客户端。这工作正常,但是当我尝试将 IHttpHandler 更改为 IHttpAsyncHandler 时,文件下载对话框显示,但文件未开始/完成下载。我做错了什么?

<%@ WebHandler Language="C#" Class="PreviewPDF"  %>

using System;
using System.Web;

public class PreviewPDF : IHttpAsyncHandler
{
    public void ProcessRequest(HttpContext context)
    {
    } 

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
    {
        string data = "some data";

        using (WebService.RequestService service = new WebService.RequestService())
        {
            AsyncCallback callback = new AsyncCallback(EndProcessRequest);
            return service.BeginGetFile(data, callback, context);
        }
    }
    public void EndProcessRequest(IAsyncResult result)
    {
        HttpContext context = result.AsyncState as HttpContext;
        byte[] wsoutput;
        using (WebService.RequestService service = new WebService.RequestService())
        {
            wsoutput = service.EndGetFile(result);
        }

        context.Response.ContentType = "application/octet-stream";
        context.Response.ContentEncoding = System.Text.Encoding.Unicode;
        context.Response.AddHeader("Content-Disposition", "attachment; filename=attachment.pdf");
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(wsoutput))
        {
            ms.WriteTo(context.Response.OutputStream);
        }
        context.Response.Flush();
    }


    public bool IsReusable {
        get {
            return false;
        }
    }
}

I'm using a IHttpHandler to call a webservice and return the resulting byte[] to the client as a downloaded file attachment. This works fine, but when I tried changing the IHttpHandler to a IHttpAsyncHandler, the file download dialog shows, but the file does not start/finish downloading. What am I doing wrong?

<%@ WebHandler Language="C#" Class="PreviewPDF"  %>

using System;
using System.Web;

public class PreviewPDF : IHttpAsyncHandler
{
    public void ProcessRequest(HttpContext context)
    {
    } 

    public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
    {
        string data = "some data";

        using (WebService.RequestService service = new WebService.RequestService())
        {
            AsyncCallback callback = new AsyncCallback(EndProcessRequest);
            return service.BeginGetFile(data, callback, context);
        }
    }
    public void EndProcessRequest(IAsyncResult result)
    {
        HttpContext context = result.AsyncState as HttpContext;
        byte[] wsoutput;
        using (WebService.RequestService service = new WebService.RequestService())
        {
            wsoutput = service.EndGetFile(result);
        }

        context.Response.ContentType = "application/octet-stream";
        context.Response.ContentEncoding = System.Text.Encoding.Unicode;
        context.Response.AddHeader("Content-Disposition", "attachment; filename=attachment.pdf");
        using (System.IO.MemoryStream ms = new System.IO.MemoryStream(wsoutput))
        {
            ms.WriteTo(context.Response.OutputStream);
        }
        context.Response.Flush();
    }


    public bool IsReusable {
        get {
            return false;
        }
    }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

睫毛溺水了 2024-08-15 16:00:34

关于您的代码的几点说明:

  1. 您需要在调用 BeginGetFile 的同一服务实例上调用 EndGetFile
  2. 您需要将 cb 作为 AsyncCallBack 传递而不是 EndProcessRequest

下面是考虑了这些注释的代码:

private class State
{
    public HttpContext Context { get; set; }
    public RequestService Service { get; set; }
}

public void ProcessRequest(HttpContext context)
{
    throw new NotImplementedException();
} 

public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
{
    // Don't use using block or it will dispose the service before you can call EndGetFile
    var state = new State
    {
        Service = new RequestService(),
        Context = context
    };
    // Pass cb here and not EndProcessRequest
    return state.Service.BeginGetFile(cb, state);
}

public void EndProcessRequest(IAsyncResult result)
{
    State state = result.AsyncState as State;
    // Be carefull as this may throw: it is best to put it in a try/finally block
    // so that you dispose properly of the service
    byte[] buffer = state.Service.EndGetFile(result);
    state.Service.Dispose();
    state.Context.Response.ContentType = "application/octet-stream";
    state.Context.Response.AddHeader("Content-Disposition", "attachment; filename=attachment.pdf");
    // Write directly into the output stream, and don't call Flush
    state.Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}

public bool IsReusable 
{ 
    get { return false; } 
}

Few remarks about your code:

  1. You need to call EndGetFile on the same service instance on which you called BeginGetFile
  2. You need to pass cb as the AsyncCallBack instead of EndProcessRequest

Here's the code with these remarks taken into account:

private class State
{
    public HttpContext Context { get; set; }
    public RequestService Service { get; set; }
}

public void ProcessRequest(HttpContext context)
{
    throw new NotImplementedException();
} 

public IAsyncResult BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
{
    // Don't use using block or it will dispose the service before you can call EndGetFile
    var state = new State
    {
        Service = new RequestService(),
        Context = context
    };
    // Pass cb here and not EndProcessRequest
    return state.Service.BeginGetFile(cb, state);
}

public void EndProcessRequest(IAsyncResult result)
{
    State state = result.AsyncState as State;
    // Be carefull as this may throw: it is best to put it in a try/finally block
    // so that you dispose properly of the service
    byte[] buffer = state.Service.EndGetFile(result);
    state.Service.Dispose();
    state.Context.Response.ContentType = "application/octet-stream";
    state.Context.Response.AddHeader("Content-Disposition", "attachment; filename=attachment.pdf");
    // Write directly into the output stream, and don't call Flush
    state.Context.Response.OutputStream.Write(buffer, 0, buffer.Length);
}

public bool IsReusable 
{ 
    get { return false; } 
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文