在 ColdFusion 中将 Base64 解码为图像
Decoding Base64 to an Image in ColdFusion
不久前,我发布了一个关于将 Base64 字符串解码为图像以存储在我的 ColdFusion 应用程序中的问题。 Here's the link.
我从 Topaz 签名板获取字符串,该签名板具有创建 Base64 字符串的 NAPI 浏览器插件。然后我将获取该字符串并使用以下代码对其进行解码:
<cfscript>
binaryValue = binaryDecode( form.SigImgData, "hex" );
FileWrite("c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp", "#binaryValue#");
</cfscript>
然后我会将其存储到磁盘并使用标签在浏览器中显示。
现在 Google Chrome 已停止支持 NAPI 插件,我现在使用签名板制造商提供的 JavaScript 创建 Base64 字符串。我可以使用新的 JS 成功创建 Base64 字符串,但是当我将该字符串插入上面的相同 CFScript 代码时,它会创建一个无法使用的图像。基本上是损坏的图像文件。
这是我的 Base64 字符串的示例:
iVBORw0KGgoAAAANSUhEUgAAAfQAAABkCAYAAABwx8J9AAAIaklEQVR4Xu3csZMMTRgH4JYJyYRkRKSykxEhQkQoQ0Z0JyJDRIZISkSGiD9BRigkk/mu96s+Y+y62b1de/O+z1YpVWe3e97nHfWb7pnbAz9//twopdQ/XgQIECBAgMBIBQ5sB/rW9rFvjvT4HTYBAgQIECCwLSDQnQYECBAgQCCAgEAP0EQlECBAgAABge4cIECAAAECAQQEeoAmKoEAAQIECAh05wABAgQIEAggINADNFEJBAgQIEBAoDsHCBAgQIBAAAGBHqCJSiBAgAABAgLdOUCAAAECBAIICPQATVQCAQIECBAQ6M6BdAKfPn0qX79+LYcOHSqnTp1aev1t/DrwquZY+kEbkACB0QsI9NG3UAHzCHz58qUcO3Zs5yPXrl0rV69eXVrw1jA/ceLEb4dU53j69Ok8h7nwe7sXEy4oFmb0QQKjFBDoo2ybg96LwMOHD8u3b9/K+/fvy7t3734L9xs3buxp1X7nzp1y//79cuHChXLy5MmdOTY2Nkodu67YVxW0W1tb5e7du3/Q1Lk3NzdL/duLAIG4AgI9bm9VtovAjx8/ypMnT/4I90VX1PUi4f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6rlwABAgRCCgj0kG1VFAECBAhkExDo2TquXgIECBAIKSDQQ7ZVUQQIECCQTUCgZ+u4egkQIEAgpIBAD9lWRREgQIBANgGBnq3j6iVAgACBkAL/AcbIWJJimEY5AAAAAElFTkSuQmCC
我找到了一个网站,它允许您将字符串传递到一个框中并将其解码为原始图像,而且它似乎解码得很好。
根据建议,我尝试了以下修改:
<cfset image = imageReadBase64("data:image/bmp;base64,form.SigImgData")>
<cfimage
source="#image#"
destination="c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp"
action="write">
但是我收到以下错误:
无法解码字符串 "form.SigImgData"。
输入字符串不是 base64 编码的。
使用 Base64 字符串,您可以使用此函数获取图像:
ImageReadBase64(yourstring)
示例:
<cfset image = ImageReadBase64(form.SigImgData)>
<cfimage source="#image#" destination="C:\Inetpub\wwwroot\signatures\#fullfilename#.bmp" action="write">
因为您的二进制数据没有图像 headers(包含 Mime 类型,例如 data:image/png;base64
用于 png
图像)所以您可以像这样简单地使用 imageReadBase64:
<cfset image = imageReadBase64(form.SigImgData)>
<cfimage
source="#image#"
destination="c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp"
action="write">
我在本地用同样的代码试过了。这是你的形象吗?
不久前,我发布了一个关于将 Base64 字符串解码为图像以存储在我的 ColdFusion 应用程序中的问题。 Here's the link.
我从 Topaz 签名板获取字符串,该签名板具有创建 Base64 字符串的 NAPI 浏览器插件。然后我将获取该字符串并使用以下代码对其进行解码:
<cfscript>
binaryValue = binaryDecode( form.SigImgData, "hex" );
FileWrite("c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp", "#binaryValue#");
</cfscript>
然后我会将其存储到磁盘并使用标签在浏览器中显示。
现在 Google Chrome 已停止支持 NAPI 插件,我现在使用签名板制造商提供的 JavaScript 创建 Base64 字符串。我可以使用新的 JS 成功创建 Base64 字符串,但是当我将该字符串插入上面的相同 CFScript 代码时,它会创建一个无法使用的图像。基本上是损坏的图像文件。
这是我的 Base64 字符串的示例:
iVBORw0KGgo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CBAgQCCkg0EO2VVEECBAgkE1AoGfruHoJECBAIKSAQA/ZVkURIECAQDYBgZ6t4+olQIAAgZACAj1kWxVFgAABAtkEBHq2jquXAAECBEIKCPSQbVUUAQIECGQTEOjZOq5eAgQIEAgpINBDtlVRBAgQIJBNQKBn67h6CRAgQCCkgEAP2VZFESBAgEA2AYGerePqJUCAAIGQAgI9ZFsVRYAAAQLZBAR6to6rlwABAgRCCgj0kG1VFAECBAhkExDo2TquXgIECBAIKSDQQ7ZVUQQIECCQTUCgZ+u4egkQIEAgpIBAD9lWRREgQIBANgGBnq3j6iVAgACBkAICPWRbFUWAAAEC2QQEeraOq5cAAQIEQgoI9JBtVRQBAgQIZBMQ6Nk6rl4CBAgQCCkg0EO2VVEECBAgkE1AoGfruHoJECBAIKSAQA/ZVkURIECAQDYBgZ6t4+olQIAAgZACAj1kWxVFgAABAtkEBHq2jquXAAECBEIKCPSQbVUUAQIECGQTEOjZOq5eAgQIEAgpINBDtlVRBAgQIJBNQKBn67h6CRAgQCCkgEAP2VZFESBAgEA2AYGerePqJUCAAIGQAgI9ZFsVRYAAAQLZBAR6to6rlwABAgRCCgj0kG1VFAECBAhkExDo2TquXgIECBAIKSDQQ7ZVUQQIECCQTUCgZ+u4egkQIEAgpIBAD9lWRREgQIBANgGBnq3j6iVAgACBkAL/AcbIWJJimEY5AAAAAElFTkSuQmCC
我找到了一个网站,它允许您将字符串传递到一个框中并将其解码为原始图像,而且它似乎解码得很好。
根据建议,我尝试了以下修改:
<cfset image = imageReadBase64("data:image/bmp;base64,form.SigImgData")>
<cfimage
source="#image#"
destination="c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp"
action="write">
但是我收到以下错误:
无法解码字符串 "form.SigImgData"。 输入字符串不是 base64 编码的。
使用 Base64 字符串,您可以使用此函数获取图像:
ImageReadBase64(yourstring)
示例:
<cfset image = ImageReadBase64(form.SigImgData)>
<cfimage source="#image#" destination="C:\Inetpub\wwwroot\signatures\#fullfilename#.bmp" action="write">
因为您的二进制数据没有图像 headers(包含 Mime 类型,例如 data:image/png;base64
用于 png
图像)所以您可以像这样简单地使用 imageReadBase64:
<cfset image = imageReadBase64(form.SigImgData)>
<cfimage
source="#image#"
destination="c:\Inetpub\wwwroot\signatures\#fullfilename#.bmp"
action="write">
我在本地用同样的代码试过了。这是你的形象吗?