我收到一个 Base64 字符串,它实际上是 PDF 文件的字符串表示形式。我想用 Response.Write 写入此字符串,但不将其转换回其二进制表示形式。
我试过这个:
var base64string = "...";
Response.Write(base64String);
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Transfer-Encoding", "base64");
浏览器无法将内容识别为 base64 编码的 PDF 文件。我怎样才能解决这个问题?
编辑:这是回应
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: application/pdf; charset=utf-8
Vary: Accept-Encoding
Server: Microsoft-IIS/7.5
Content-Transfer-Encoding: base64
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Wed, 11 Apr 2012 11:00:04 GMT
Content-Length: 107304
JVBERi0xLjQKJeLjz9MKMSA... more content here
Content-Transfer-Encoding
不是有效的 HTTP 标头;这是 MIME 中的旧标头。它的 HTTP 等价物是Transfer-Encoding
它支持以下值:
- chunked
- identity
- gzip
- compress
- deflate
如果您有 Base64 编码的 PDF 文档,则 HTTP 中没有“来自 base64”转换来为您解码此文档,因此您must在将其放入响应正文之前,在您的服务器上对其进行解码。
如果您想要从 Base64 转换的流,您可以使用FromBase64Transform http://msdn.microsoft.com/en-us/library/system.security.cryptography.frombase64transform.aspx into a CryptoStream http://msdn.microsoft.com/en-us/library/system.security.cryptography.cryptostream.aspx:
new CryptoStream(fileStream, new FromBase64Transform(), CryptoStreamMode.Read)
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)