Base64编码的Response.Write字符串 [英] Response.Write Base64 string

查看:842
本文介绍了Base64编码的Response.Write字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到一个Base64字符串,它实际上是重新PDF文件的presentation字符串。我想写此字符串的Response.Write,但没有将其转换回其二进制重新presentation。

我试过这样:

  VAR base64string =...;
的Response.Write(base64String);
Response.ContentType =应用程序/ PDF
Response.AddHeader(内容传输编码,BASE64);

浏览器不能识别内容为Base64 EN codeD PDF文件。我该如何解决这个问题?

编辑:这是响应

  HTTP / 1.1 200 OK
缓存控制:私人
内容类型:应用程序/ PDF格式;字符集= UTF-8
有所不同:接受编码
服务器:Microsoft-IIS / 7.5
内容传输编码:BASE64
的X ASPNET-版本:4.0.30319
的X已启动方式:ASP.NET
日期:星期三,2012年4月11日11时00分04秒GMT
内容长度:107304JVBERi0xLjQKJeLjz9MKMSA ...更多内容点击这里


解决方案

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


  • 分块

  • 身份

  • 的gzip

  • COM preSS

  • 放气

如果你有一个Base64连接codeD的PDF文件,还没有一个从BASE64改造中的HTTP将取消code这个文件给你,所以你的必须德code您的服务器上,之前把它在响应体。

如果您想从Base64编码转换流,你可以使用的 FromBase64Transform 成的 的CryptoStream

 新的CryptoStream(FILESTREAM,新FromBase64Transform(),CryptoStreamMode.Read)

I receive a Base64 string which is actually the string representation of a PDF file. I want to write this string with Response.Write, but without converting it back to its binary representation.

I tried this:

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

The browser does not recognize the content as a base64 encoded PDF file. How can I fix this?

EDIT: this is the response

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 is not a valid HTTP header; this is an old header from MIME. It's HTTP equivalent is Transfer-Encoding which supports the following values:

  • chunked
  • identity
  • gzip
  • compress
  • deflate

If you have a Base64 encoded PDF document, there isn't a "from base64" transform in HTTP which will decode this document for you, so you must decode it on your server, prior to putting it in the response body.

If you want a stream that converts from Base64, you can use a FromBase64Transform into a CryptoStream:

new CryptoStream(fileStream, new FromBase64Transform(), CryptoStreamMode.Read)

这篇关于Base64编码的Response.Write字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆