<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
</head>
<body>
<script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
<script type="text/javascript">
// 读取图片文件的base64数据,并回调
function ReadFile(file, callback) {
// 实例化一个FileReader实例
var reader = new FileReader();
reader.onload = function() {
file_data = this.result;
file_data = file_data.split(",");
file_data = file_data.pop();
// 执行回调函数,传递图片base64数据
callback(file_data);
};
reader.readAsDataURL(file);
}
// 入口函数
function SelectFile(){
var postUrl = "http://www.bingtop.com/ocr/upload/";
var param = {
username : 'admin',
password : '123456',
captchaType: 1000
};
var form = document.forms["fileform"];
if (form["file"].files.length > 0) {
var file = form["file"].files[0];
ReadFile(file, function(img_data){
param["captchaData"] = img_data;
// 发起post请求,获取验证结果
// result格式示例 {"code":0, "message":"", "data":{"captchaId":"1001-158201918112812","recognition":"RESULT"}}
$.post(postUrl, param, function(result){
code_id = result['data']['captchaId'];
code = result['data']['recognition'];
console.log('当前识别的验证码为:' + code)
});
});
}
}
</script>
<style type="text/css">
label{display: block;margin-bottom: 10px}
</style>
<form style="border: 1px solid #ccc; padding: 20px; border-radius: 10px; width: 500px;margin:auto" name= "fileform" id="fileform" method="post" enctype="multipart/form-data" action="javascript:SelectFile();">
<label>账号:<input type="text" name="username"></label>
<label>密码:<input type="password" name="password"></label>
<label>类型:<input type="text" name="captchaType" value="1001" > 图文类型</label>
<label><input type='file' name="file" /></label>
<input type="submit" value="识别" />
</form>
</body>
</html>