【问题标题】:C# Web API Response Type for a pdfpdf 的 C# Web API 响应类型
【发布时间】:2018-08-22 04:43:32
【问题描述】:

我知道这很简单,但我正在失去理智。我正在生成一个 pdf 流(或任何你想调用的流)并希望它进入网络浏览器。我希望我需要向浏览器返回一个字节数组(如果我不正确,请告诉我)。但我还没有弄清楚将响应类型设置为什么。请帮忙。

[HttpGet]
[ActionName("GetPrint")]
[ResponseType(typeof(HttpResponseMessage))] //DOESN'T WORK
public IHttpActionResult GetPrint(string templateName, int personId, int badgeId, string authToken, string ipAddress)
{
    var bdgBl = PeliquinIOC.Instance.Resolve<IBadgeDesignBL>(UserId, UserName, PropertyCode, PartitionName, IpAddress);
    //var apiRsp = new PeliquinApiRsp();

    if (!(IsAllowed(SysPrivConstants.SYSPRIV__TYPE_PERSONNEL, templateName, SysPrivConstants.SYSPRIV__LEVEL_READ)))
    {
        return (Unauthorized());
    }

    var bdgDto = bdgBl.GetPrint(templateName, personId, badgeId);

    if (bdgDto == null)
    {
        return (Unauthorized());
    }

    var myRspMsg = new HttpResponseMessage(HttpStatusCode.OK);
    myRspMsg.Content = new StreamContent(new MemoryStream(bdgDto));
    myRspMsg.Content.Headers.ContentType = new MediaTypeHeaderValue("application/pdf");

    return myRspMsg;
}

以防万一你问我是如何生成 pdf 的(图像创建绝对有效,因为我们在其他地方使用它):

public byte[] GetPrint(string templateName, int personId, int badgeId)
{
    var bdgDesignDto = new BadgeDesignDTO();
    bdgDesignDto.BdgLayoutFront = Get_BadgeLayout_Front(templateName, personId, badgeId);
    bdgDesignDto.BdgLayoutBack = Get_BadgeLayout_Back(bdgDesignDto.BdgLayoutFront, personId, badgeId);

    //Generate the images for Front and Back
    bdgDesignDto.BdgFrontImage = CreateImage(bdgDesignDto.BdgLayoutFront);
    bdgDesignDto.BdgBackImage = CreateImage(bdgDesignDto.BdgLayoutBack);

    var document = new Document(PageSize.A4, 0, 0, 0, 0);
    var msReport = new MemoryStream();
    var writer = PdfWriter.GetInstance(document, msReport); //required to write to msReport stream

    document.Open();
    document.NewPage();
    var jpg = iTextSharp.text.Image.GetInstance(bdgDesignDto.BdgFrontImage);
    jpg.SetAbsolutePosition(1,1);
    document.Add(jpg);

    document.NewPage();
    jpg = iTextSharp.text.Image.GetInstance(bdgDesignDto.BdgBackImage);
    jpg.SetAbsolutePosition(1,1);
    document.Add(jpg);

    return msReport.ToArray();  //Maybe this works?
} 

【问题讨论】:

  • 你用的是什么版本的asp.net?
  • 忽略这句话,因为它的长度必须 >= 12 个字符:.NET 4.6
  • 我怀疑这是框架版本,与 ASP.NET 版本不同。您能否在 web.config 中查找 System.Web.Mvc 的dependentAssembly 元素。它应该告诉你版本。

标签: c# asp.net pdf itext


【解决方案1】:

一种选择是使用控制器中的 ControllerBase File() 方法,该方法返回 Microsoft.AspNetCore.Mvc.FileContentResult。它会将byte[] 设置为适当的响应内容。

这样实现

[HttpGet]
[ActionName("GetPrint")]
[ResponseType(typeof(HttpResponseMessage))]
public IHttpActionResult GetPrint(string templateName, int personId, int badgeId, string authToken, string ipAddress)
{
    var bdgBl = PeliquinIOC.Instance.Resolve<IBadgeDesignBL>(UserId, UserName, PropertyCode, PartitionName, IpAddress);
    //var apiRsp = new PeliquinApiRsp();

    if (!(IsAllowed(SysPrivConstants.SYSPRIV__TYPE_PERSONNEL, templateName, SysPrivConstants.SYSPRIV__LEVEL_READ)))
    {
        return (Unauthorized());
    }

    var bdgDto = bdgBl.GetPrint(templateName, personId, badgeId);

    if (bdgDto == null)
    {
        return (Unauthorized());
    }

    return File(bdgDto, "application/pdf", "yourPdfName.pdf");
}

【讨论】:

  • 导致错误“Non-invokable File cannot be used like a method”
  • @JonathanHansen 很可能,“您的”文件指的是 System.IO.File。您需要明确指出对 System.Web.MVC.File 或 Microsoft.AspNetCore.Mvc.File 的引用
  • 好的,它不是 System.IO.File 是完全合理的。但是,没有 System.Web.MVC.File(或类似的)或 Microsoft.AspNetCore.Mvc.File 这样的东西。我搜索了相似之处,但没有找到任何相似之处。
【解决方案2】:

这是可行的解决方案:

    [System.Web.Http.HttpGet]
    [System.Web.Http.ActionName("GetPrint")]
    //[ResponseType(typeof(HttpResponseMessage))]
    public HttpResponseMessage GetPrint(string templateName, int personId, int badgeId, string authToken, string ipAddress)
    {
        var bdgBl = PeliquinIOC.Instance.Resolve<IBadgeDesignBL>(UserId, UserName, PropertyCode, PartitionName, IpAddress);

        //if (!(IsAllowed(SysPrivConstants.SYSPRIV__TYPE_PERSONNEL, templateName, SysPrivConstants.SYSPRIV__LEVEL_READ)))
        //{
        //    return (Unauthorized());
        //}

        var bdgDto = bdgBl.GetPrint(templateName, personId, badgeId);

        var result = new HttpResponseMessage(HttpStatusCode.OK)
        {
            Content = new ByteArrayContent(bdgDto)
        };
        result.Content.Headers.ContentDisposition =
            new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
            {
                FileName = $"badge_{badgeId}.pdf"
            };
        result.Content.Headers.ContentType =
            new MediaTypeHeaderValue("application/octet-stream");

        return result;
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-04-10
    • 1970-01-01
    • 2019-10-09
    • 2015-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多