【发布时间】: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 元素。它应该告诉你版本。