简体   繁体   English

如何使用经典 ASP 保存画布图像?

[英]How to save canvas image using classic 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.我知道我可以使用 canvas.toDataURL 生成一个 base64 编码的字符串以传递到我服务器上的经典 ASP 页面。 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.但我似乎无法找到答案的问题是如何处理这些数据,以便我可以将其保存在我的服务器上的某个位置。

So with this snippet of code on my HTML page, I pull the canvas data (I pulled this from another post here at StackOverflow):因此,使用 HTML 页面上的这段代码,我提取了画布数据(我从 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);
}); 

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.但是我现在不清楚一旦我在服务器端如何处理数据......我可以提取 Request.Form 元素,但我似乎无法找到一种好方法来对它进行 base64 解码,或者甚至将它输出为二进制文件...我想我听说经典 ASP 不擅长进行 base64 解码,在另一个测试中我确实找到了一个进行 base64 解码的函数,但我不知道如果它真的有效,但它确实需要很长时间才能运行。 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.我还在这里找到了这个链接: base64 imagedecoder for ASP classic ,但我想这是微软不推荐使用的 32 位组件......我想我正在向社区寻求关于节省 html5 画布的建议镜像到服务器。

You could use an XML element specifying bin.base64 data type that created through a DomDocument instance to encoding / decoding Base64 data.你可以使用一个XML元素指定bin.base64通过一个创建的数据类型DomDocument实例编码/解码的Base64数据。
Then you can save obtained binary to disk using a Stream object.然后您可以使用Stream对象将获得的二进制文件保存到磁盘。
Both of these libraries are 64 bit supported.这两个库都支持 64 位。 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.假设您发送的内容将在服务器端的请求集合(没有 json 等的经典 post 方法)中可用,以下代码可以解决问题,或者最坏的情况,我相信这会给您带来洞察力。

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

just thought I'd share a solution to this.只是想我会分享一个解决方案。

$(document).ready(function(){
    getBase64FromImageUrl('test5px.png');

            function getBase64FromImageUrl(URL) {
                var img = new Image();
                img.src = URL;
                img.onload = function () {          
                var canvas = document.createElement("canvas");
                canvas.width =this.width;
                canvas.height =this.height;         
                var ctx = canvas.getContext("2d");
                ctx.drawImage(this, 0, 0);          
                var dataURL = canvas.toDataURL("image/png");            
                //alert(  dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
                saveImageData(dataURL.replace(/^data:image\/(png|jpg);base64,/, ""));
                }
            }

            function saveImageData (image_data) {
                $.post("saveImage.asp",
                {imageData:image_data,submit:true})
                .done(function(data, textStatus, jqXHR) 
                    {
                    alert(data);             
                    }).fail(function(jqXHR, textStatus, errorThrown) 
                {
                    alert(textStatus);
                });
 });

in html在 html 中

<img id="test" src="http://someDomain.com/img/test5px.png">

I was actually getting the image data as a snapshot from a webcam canvas.toDataURL, but it works for just an img tag on a page as well我实际上是从网络摄像头 canvas.toDataURL 获取作为快照的图像数据,但它也仅适用于页面上的 img 标签

In saveImage.asp在 saveImage.asp

<%@ Language=VBScript %>
<% Option Explicit %>
<%

response.write(request("imageData"))

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("userImages/" & request("imageName")), 2                    'adSaveCreateOverWrite
%>

my answer is based on the first answer, with some modifications that made it actually work: first, in the client side we use the following function:我的答案基于第一个答案,并进行了一些修改使其实际工作:首先,在客户端我们使用以下函数:

        function onSave(){
        $.ajax({
          type: "POST",
          url: "saveSignature.asp",
          data: { 
             imgBase64: document.getElementById("eSignatureBoard").toDataURL(),
          }
        })
        .fail(function(oErr,sErr1,sErr2) {
            alert ("err - "+sErr1+"  "+sErr2);
        })    
        .done(function(){
            alert ("done");
        });   
    }

and in the asp server side we use the following code:在 asp 服务器端,我们使用以下代码:

dim sImg
sImg=request.form("imgBase64")
sImg = replace(sImg,"data:image/png;base64,", "")
sImg = replace(sImg," ", "+")

Set oStream = Server.CreateObject("Adodb.Stream")
oStream.Type = 1 
oStream.Open
With Server.CreateObject("MSXML2.DomDocument.6.0").createElement("b64")
    .dataType = "bin.base64"
    .text = sImg
    oStream.Write .nodeTypedValue 'write bytes of decoded base64 to stream
End With
oStream.SaveToFile Server.Mappath ("./tmp_08221_1.png"), 2
oStream.close()
set oStream = nothing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM