问题描述
我有点卡在这里.我知道我可以使用canvas.toDataURL生成base64编码的字符串,以传递到服务器上的经典ASP页面.但是我似乎找不到答案的问题是如何处理这些数据,以便可以将其保存在服务器上的某个位置.
I'm a bit stuck here. I know that I can use the canvas.toDataURL to produce a base64 encoded string to pass to a classic ASP page on my server. But the problem I can't seem to find an answer to is how to process this data so I can save it someplace on my server.
因此,通过HTML页面上的这段代码,我提取了画布数据(是从StackOverflow上的另一篇文章中提取的):
So with this snippet of code on my HTML page, I pull the canvas data (I pulled this from another post here at StackOverflow):
var dataURL = renderedCanvas.toDataURL("image/png");
dataURL = dataURL.replace('data:image/png;base64,', '');
var areturn = $.ajax({
url: "http://127.0.0.1/mySite/saveImage.asp",
type: "POST",
data: '{ "imageData" : "' + dataURL + '" }',
dataType: "json",
beforeSend: function(x) {
x.overrideMimeType("application/j-son;charset=UTF-8");
}
}).done(function(result) {
console.log("Success Done!\n" + result);
}).always(function(data) {
console.log("Always:\n" + data.responseText);
});
但是我现在不清楚我在服务器端后如何处理数据...我可以拉出Request.Form元素,但是我似乎都找不到找到一种方法来进行base64解码它,甚至将其输出为二进制文件...我想我听说经典的ASP并不能很好地进行base64解码,在另一项测试中,我确实找到了可以对base64进行解码的函数,但是我无法不能确定它是否真的有效,但是它确实需要很长时间才能运行.我还在这里找到此链接: ASP经典的base64图像解码器,但是我猜想这是Microsoft不推荐使用的32位组件...我想我正在向这里的社区寻求有关将html5画布图像保存到服务器上的建议.
But I'm unclear now what to do with the data once I'm on the server side... I can pull the Request.Form element, but I can't seem to find a good way to either base64 decode it, or even output it as a binary file... I guess I've heard that classic ASP isn't any good at doing base64 decoding, and in another test I did find a function that did the base64 decode, but I couldn't tell if it really worked, but it did take a long time to run.I also found this link here: base64 image decoder for ASP classic, but I guess this is a 32bit component that Microsoft doesn't recommend using... I guess I'm looking to the community here for suggestions on saving out an html5 canvas image onto the server.
推荐答案
您可以使用XML元素,该元素指定通过DomDocument
实例创建的bin.base64
数据类型,以对Base64数据进行编码/解码.
然后,您可以使用Stream
对象将获得的二进制文件保存到磁盘.
这两个库都支持64位.假设您发送的内容将在服务器端的Request集合(没有json等的经典发布方法)中可用,那么以下代码可以解决问题,或者在最坏的情况下,我相信这可以为您提供见识.
You could use an XML element specifying bin.base64
data type that created through a DomDocument
instance to encoding / decoding Base64 data.
Then you can save obtained binary to disk using a Stream
object.
Both of these libraries are 64 bit supported. Assuming the content you sent will be available in a Request collection (classic post methods without json etc.) on the server-side, following code solves the problem or at worst I'm sure that gives you insight.
saveImage.asp
Function Base64Data2Stream(sData)
Set Base64Data2Stream = Server.CreateObject("Adodb.Stream")
Base64Data2Stream.Type = 1 'adTypeBinary
Base64Data2Stream.Open
With Server.CreateObject("MSXML2.DomDocument.6.0").createElement("b64")
.dataType = "bin.base64"
.text = sData
Base64Data2Stream.Write .nodeTypedValue 'write bytes of decoded base64 to stream
Base64Data2Stream.Position = 0
End With
End Function
Dim CanvasStream
Set CanvasStream = Base64Data2Stream(Request.Form("imageData"))
'Write binary to Response Stream
'Response.BinaryWrite CanvasStream.Read
'Write binary to File
CanvasStream.SaveToFile Server.Mappath("imgFileFromCanvas.png"), 2 'adSaveCreateOverWrite
这篇关于如何使用经典ASP保存画布图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!