<input type="file" name="img-up" id="img-up" value="" />
$("#img-up").on("change",function () {
if(this.files[0]){
getImgBase64Data(this.files[0], function (result) {
console.log(result);
});
}
});
function getImgBase64Data(file, callback) {
var reader = new FileReader();
reader.onload = function (e) {
callback(e.target.result);
};
reader.readAsDataURL(file); // 读取完后会调用onload方法
}
方法二:
//取得该文件的url
function getObjectURL(file) {
var url = null ;
if (window.createObjectURL!=undefined) {
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) {
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) {
url = window.webkitURL.createObjectURL(file) ;
}
return url ;
}