ASP图片验证码转成Base64数据

sharembweb 28次浏览

摘要:ASP图片格式与base64数据相互转换的方法,经常用于处理表单中存储有base64字符串格式的图片

使用ASP方法获取base64数据,转换成图片

ASP代码部分:点击预览
<% 
'************************************************************
'作者:云祥孙 (精通ASP/VB/PHP/JS/Flash,交流合作可联系本人)
'版权:源代码公开,各种用途均可免费使用。
'创建:2016-01-15
'联系:QQ313801120  交流群35915100(群里已有几百人)    邮箱313801120@qq.com   个人主页 sharembweb.com
'更多帮助,文档,更新 请加群(35915100)或浏览(sharembweb.com)获得
'*                                    Powered By 云祥孙
'************************************************************

Dim url 
Session("yzm") = getRnd(4) 
Response.Write("yzm=" & Session("yzm") & "<br>") 
url = "http://a.sharembweb.com//ServiceTest/testYZM.Asp?yzm=" & Session("yzm") 
Call downRemoteFileToBase64(url) 

Function downRemoteFileToBase64(ByVal remoteFileurl)
    Dim xmlHttp, getRemoteData 
    Set xmlHttp = CreateObject("Microsoft.XMLHTTP")
        With xmlHttp
            Call.Open("Get", remoteFileurl, False, "", "") 'Flase同步,True异步
            .send 
            getRemoteData =.responseBody 
        End With 
    Set xmlHttp = Nothing 

    Set objXMLDoc = CreateObject("msxml2.FreeThreadedDOMDocument")
        '设定生成XML文档的根为 Base64Data
        Call objXMLDoc.loadxml("<?xml version='1.0'?><Base64Data />") 
        objXMLDoc.documentElement.dataType = "bin.base64" 
        objXMLDoc.documentElement.nodeTypedValue = getRemoteData 
        Set xml = CreateObject("msxml2.FreeThreadedDOMDocument")
            Call xml.load(objXMLDoc) 
            If xml.readyState > 2 Then
                Set root = xml.getElementsByTagName("Base64Data")
                    base64 = "data:image/" & getFileExt & ";base64," & Replace(root.Item(0).text, Chr(10), "") 
                    Response.Write(base64) 
                Set root = Nothing 
            End If 

        Set xml = Nothing 

    Set objXMLDoc = Nothing 
End Function
 
'生成随机数
Function getRnd(ByVal nCount)
    Randomize 
    Dim charS, i, c 
    For i = 1 To nCount
        charS = ChrW((57 - 48) * Rnd + 48)  '0~9
        c = c & charS 
    Next 
    getRnd = c 
End Function
%> 

使用JS方法获取base64数据,转换成图片

ASP代码部分:点击预览
<img src="" id="zym" onclick="imgtobase64('http://a.sharembweb.com/ServiceTest/YZM_7.Asp')" style="cursor:hand;"> 
<script language="javascript">  
function getBase64Image(img) {  
     var canvas = document.createElement("canvas");  
     canvas.width = img.width;  
     canvas.height = img.height;  
     var ctx = canvas.getContext("2d");  
     ctx.drawImage(img, 0, 0, img.width, img.height);  
     var ext = img.src.substring(img.src.lastIndexOf(".")+1).toLowerCase();  
     var dataURL = canvas.toDataURL("image/"+ext);  
     return dataURL;  
}
function imgtobase64(img){
	img+="?n="+Math.random()  
	var image = new Image();  
	image.src = img;  
	image.onload = function(){  
	  var base64 = getBase64Image(image);     
	  document.getElementById("zym").src=base64 
	}  
} 
document.getElementById("zym").onclick();
</script>

本文版权归作者所有,转摘请注明作者和出处!
作者:云祥孙
首发:http://sharembweb.com

随机内容

表情

共2条评论
  • 网友评论:

    赞一个

    2020-07-24 16:26:24 回复

  • 网友评论:

    学习了

    2020-07-24 16:26:24 回复

友情链接