首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >通过IHttpAsyncHandler发送文件时出现问题

通过IHttpAsyncHandler发送文件时出现问题
EN

Stack Overflow用户
提问于 2009-10-28 02:30:29
回答 1查看 2K关注 0票数 3

我使用IHttpHandler调用and服务,并将生成的byte[]作为下载的文件附件返回给客户端。这可以很好地工作,但当我尝试将IHttpHandler更改为IHttpAsyncHandler时,会显示文件下载对话框,但文件无法开始/完成下载。我做错了什么?

代码语言:javascript
复制
<%@ 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;
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2009-10-28 03:53:29

关于您的代码的几点备注:

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

作为AsyncCallBack传递

以下是考虑到这些备注的代码:

代码语言:javascript
复制
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; } 
}
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/1632782

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档