var
imgFile = null;
//绑定监听选择图片事件
$('input[type="file"]').change(function(event){
var reader = new FileReader();
reader.onload = (function(inputDom) {
//
inputDom用于保存'input[type="file"]'的dom对象
return function(e) {
imgFile = e.target.result;
//显示在页面上
var imgUrl = 'data:image/jpg;base64,'+arrayBufferToBase64(imgFile);
$('img').attr('src',
imgUrl);
};
})(this);
var file = event.target.files[0];
reader.readAsArrayBuffer(file);
});
//ajax传送文件
$.ajax({
url: uploadURL,
type:"PUT",
async: false,
data:
imgFile,
contentType: false,
processData: false,
success:function(resp){
},
error:function(){
}
});
function arrayBufferToBase64( buffer ) {
var binary = '';
var bytes = new Uint8Array( buffer );
var len = bytes.byteLength;
for (var i = 0; i < len; i++) {
binary += String.fromCharCode( bytes[ i ] );
}
return window.btoa( binary );
}
function base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array( len );
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
阅读(479) | 评论(0) | 转发(0) |