C# 响应。写入Base64字符串

C# 响应。写入Base64字符串,c#,asp.net,http,C#,Asp.net,Http,我收到一个Base64字符串,它实际上是PDF文件的字符串表示形式。我想用Response.write编写这个字符串,但不将其转换回二进制表示形式 我试过这个: var base64string = "..."; Response.Write(base64String); Response.ContentType = "application/pdf"; Response.AddHeader("Content-Transfer-Encoding", "base64"); 浏览器无法将内容识别为

我收到一个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

当你承诺使用PDF格式时

 Response.ContentType = "application/pdf";
您还应该通过打开响应流并在那里编写PDF的二进制版本来交付一个响应流

你能试试这个吗

var base64string = "...";
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Transfer-Encoding", "base64");
Response.Write(base64String);
Response.End();

这可能会帮助您

内容传输编码
不是有效的HTTP头;这是来自MIME的旧标题。它的HTTP等价物是
传输编码
,它支持以下值:

  • 大块
  • 身份
  • gzip
  • 压缩
  • 泄气
如果您有一个Base64编码的PDF文档,HTTP中没有“from Base64”转换将为您解码此文档,因此您必须在服务器上对其进行解码,然后才能将其放入响应正文中

如果需要从Base64转换的流,可以使用转换为:


您是否尝试过
Response.BinaryWrite(fileContent)
?@ShadowWizard,fileContet不是byte,是base64字符串。我认为在使用
响应之前需要添加头/类型。写入
?@pjumble,顺序无关紧要(因为我不结束响应)。我用fiddler检查过,它写标题(好的),但没有将其转换回二进制表示形式-你想把它留给浏览器吗?为什么?谢谢你的贡献。而且二进制数据更轻,这很有意义。由于SMTP只与ASCII一起工作,因此引入了base64以这种形式传输二进制数据,但HTTP理解二进制,因此不需要使用base64。
new CryptoStream(fileStream, new FromBase64Transform(), CryptoStreamMode.Read)