window.onload = function() {
var input = document.getElementById("pic");
var result = document.getElementById("result");
// var img_area = document.getElementById("img_area");
if (typeof (FileReader) === 'undefined') {
alert("抱歉,你的浏览器不支持 FileReader,请使用现代浏览器操作!");
input.setAttribute('disabled', 'disabled');
} else {
input.addEventListener('change', readFile, false);
}
}
function readFile() {
var file = this.files[0];
//这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件
if (!/image\/\w+/.test(file.type)) {
alert("请确保文件为图像类型");
return false;
}
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function(e) {
result.innerHTML = this.result;
updataImg();
}
}
function updataImg() {
//var picServer = "http://203.88.202.43:9099";
//var rServer = "http://172.31.211.153:8080/";//
var rServer = "http://203.88.202.43:9088/";
var path = "api/Picture/UploadPic";
var a = $("#result").val();
$("#result").text("");
var photo = a.substr(a.indexOf(",") + 1, a.length - 1);
var param = {
userid : "1",
picdata : photo,
type : "2"
};
console.log(param);
$.ajax({
url : rServer + path,
type : "post",
data : param,
dataType : "json",
async : false,
success : function(data) {
if(data.status=="true"){
console.log(data);
$("#p").attr("src",picServer+data.data);
$("#picture").val(data.data);
alert("上传成功!");
}
},
error : function() {
alert("上传失败!");
}
});
}