[2 [2
filecontentresult 在MVC控制器中效果很好,可用于服务PDF之类的文件,直接将其移植到
提出挑战。 使用 streamContent 的简单尝试通常会失败,导致JSON Metadata而不是文件本身。 该解决方案在于利用
bytearrayContent。
此修订的代码段有效地返回PDF文件作为文件内容从Web API产生的文件:
[2
公共httpresponsemessage生成()
{
使用(var stream = new MemoryStream())
{
//处理流以在此处生成PDF内容的流...
变量结果=新的httpresponsemessage(httpstatuscode.ok)
{
content = new bytearraycontent(stream.toarray())
};
result.content.headers.contentdisposition = new ContentDisPositionHeaderValue(“ attactment”)
{
filename =“ certificationcard.pdf”
};
result.content.headers.contenttype = new MediaTypeHeaderValue(“应用/八位字节”);
返回结果;
}
}
bytearrayContent
封装文件字节,并设置 contentType
标头确保客户端的正确处理。 请注意使用
的使用来确保正确处理
。 这种方法可以通过您的Web API无缝地交付PDF和其他文件类型。免责声明: 提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发到邮箱:[email protected] 我们会第一时间内为您处理。
Copyright© 2022 湘ICP备2022001581号-3