如何在API中返回pdf文件并在angular4的帮助下显示

时间:2017-11-08 12:19:48

标签: angular c#-4.0 angularjs-directive

我想创建API以返回字节流并在angular4

的帮助下显示

我也在asp中创建了一个Handler方法然后该地方工作但是在API中我不知道如何返回一个字节请解释或建议我

这里的代码是处理程序

  

public void ProcessRequest(HttpContext context)       {           尝试           {               string pdfName = context.Request.QueryString [" Name"]。ToString();

        var fullName = HttpContext.Current.Server.UrlDecode(pdfName);
        byte[] bytes;
        string fileName = System.IO.Path.GetFileName(pdfName);
        //string file = context.Request.QueryString["fileName"].ToString();

        bytes = File.ReadAllBytes(fullName);
        context.Response.Buffer = true;
        context.Response.Charset = "";
        if (context.Request.QueryString["download"] == "1")
        {
            context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
        }
        context.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        context.Response.ContentType = "application/pdf";
        context.Response.BinaryWrite(bytes);
        context.Response.Flush();
        context.Response.Clear();
        context.ApplicationInstance.CompleteRequest();
    }
    catch (Exception ex)
    {
        throw;//ErrorSignal.FromCurrentContext().Raise(ex);
    }
}

public bool IsReusable
{
    get
    {
        return false;
    }
}

0 个答案:

没有答案