coldfusion中的Base64解码错误

时间:2017-07-25 13:28:46

标签: coldfusion

我正在尝试使用#ToString( ToBinary( stringToDecode ) )#解码base64字符串,而coldfusion会出错,说该参数必须是base-64编码的字符串。该字符串来自第三方,应该是一个pdf文件。以下是其中的一部分:

JVBERi0xLjUKJeLjz9MKMSAwIG9iago8PC9UeXBlL1hPYmplY3QvUmVzb3VyY2VzPDw+Pi9TdWJ0 eXBlL0Zvcm0vQkJveFswIDAgMTUuNDQgMTUuNDZdL01hdHJpeCBbMSAwIDAgMSAwIDBdL0xlbmd0 aCAyMi9Gb3JtVHlwZSAxL0ZpbHRlci9GbGF0ZURlY29kZT4+c3RyZWFtCk9lHxbaEDXZpIO1A/Gj mGuxQdW3qkgKZW5kc3RyZWFtCmVuZG9iagoyIDAgb2JqCjw8L1R5cGUvWE9iamVjdC9SZXNvdXJj ZXM8PD4+L1N1YnR5cGUvRm9ybS9CQm94WzAgMCAxNS40NCAxNS4xOV0vTWF0cml4IFsxIDAgMCAx IDAgMF0vTGVuZ3RoIDIyL0Zvcm1UeXBlIDEvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KtDN8 NkUKYx04Gj8V9LfOboGvN9VRIgplbmRzdHJlYW0KZW5kb2JqCjMgMCBvYmoKPDwvVHlwZS9YT2Jq ZWN0L1Jlc291cmNlczw8Pj4vU3VidHlwZS9Gb3JtL0JCb3hbMCAwIDE1LjQ0IDE1LjcyXS9NYXRy aXggWzEgMCAwIDEgMCAwXS9MZW5ndGggMjIvRm9ybVR5cGUgMS9GaWx0ZXIvRmxhdGVEZWNvZGU+ 

我曾经想过的是空间是一个问题,但我做了一个替换,用“”替换所有“”,他们仍在那里。还有什么我应该做的吗?

2 个答案:

答案 0 :(得分:1)

在我的测试中,我可以使用本地pdf并将其转换为字符串,以模仿您从API获得的字符串。然后我可以把它转换回PDF。

当我使用上面的字符串时,我无法使其工作。不确定是否列出了您在此处粘贴的内容。

这是我的代码,也许你可以改变它以使用api中的字符串。

<cffile action="readbinary" file="#expandPath('./test.pdf')#" variable="thefile"/>

<cfset the_string = toString(theFile)/>

<cfscript>
patrick_string = ToBase64("JVBERi0xLjUKJeLjz9MKMSAwIG9iago8PC9UeXBlL1hPYmplY3QvUmVzb3VyY2VzPDw+Pi9TdWJ0 eXBlL0Zvcm0vQkJveFswIDAgMTUuNDQgMTUuNDZdL01hdHJpeCBbMSAwIDAgMSAwIDBdL0xlbmd0 aCAyMi9Gb3JtVHlwZSAxL0ZpbHRlci9GbGF0ZURlY29kZT4+c3RyZWFtCk9lHxbaEDXZpIO1A/Gj mGuxQdW3qkgKZW5kc3RyZWFtCmVuZG9iagoyIDAgb2JqCjw8L1R5cGUvWE9iamVjdC9SZXNvdXJj ZXM8PD4+L1N1YnR5cGUvRm9ybS9CQm94WzAgMCAxNS40NCAxNS4xOV0vTWF0cml4IFsxIDAgMCAx IDAgMF0vTGVuZ3RoIDIyL0Zvcm1UeXBlIDEvRmlsdGVyL0ZsYXRlRGVjb2RlPj5zdHJlYW0KtDN8 NkUKYx04Gj8V9LfOboGvN9VRIgplbmRzdHJlYW0KZW5kb2JqCjMgMCBvYmoKPDwvVHlwZS9YT2Jq ZWN0L1Jlc291cmNlczw8Pj4vU3VidHlwZS9Gb3JtL0JCb3hbMCAwIDE1LjQ0IDE1LjcyXS9NYXRy aXggWzEgMCAwIDEgMCAwXS9MZW5ndGggMjIvRm9ybVR5cGUgMS9GaWx0ZXIvRmxhdGVEZWNvZGU+");

string2 = toBinary(ToBase64(the_string));

binencode=BinaryEncode(string2, "Base64"); 

</cfscript>


<cffile action="write" file="#expandPath('./randy.pdf')#" output="#thefile#" addnewline="No" />

<a href="randy.pdf">click</a>

更新代码:

<cffile action="readbinary" file="#expandPath('./test.pdf')#" variable="thefile"/>

<cfset the_string = toBase64(theFile)/>

<cfscript>

string2 = toBinary(the_string);

</cfscript>


<cffile action="write" file="#expandPath('./randy.pdf')#" output="#string2#" addnewline="No" />

<a href="randy.pdf">click</a>

答案 1 :(得分:1)

这是更新的 cfscript 方法,以防有人感兴趣:

<cfscript>
// read the binary file and save it as a variable
myPdf = fileReadBinary( expandPath( "/test.pdf" ) );

// encode as base64
myPdf = toBase64( myPdf );

// output the base64 version to the browser
writedump( myPdf );

// convert back to binary
myPdf = toBinary( myPdf );

// write to a new PDF file
fileWrite( expandPath( "/new.pdf" ), myPdf );
</cfscript>