首页 > 代码库 > h5 js 图片预览并判断

h5 js 图片预览并判断

//建立一個可存取到該file的url
function getObjectURL(file) {
    var url = null;
    if (window.createObjectURL != undefined) { // basic
        url = window.createObjectURL(file);
    } else if (window.URL != undefined) { // mozilla(firefox)
        url = window.URL.createObjectURL(file);
    } else if (window.webkitURL != undefined) { // webkit or chrome
        url = window.webkitURL.createObjectURL(file);
    }
    return url;
}
$(document).on(‘change‘, ‘.wishfile‘, function () {
    var _upload_file = $(this).val().toLowerCase();
    var _upload_file_hz = _upload_file.substr(_upload_file.lastIndexOf(‘.‘) + 1);
    var _upload_file = [‘jpg‘, ‘png‘, ‘gif‘, ‘jpeg‘];
    if ($.inArray(_upload_file_hz, _upload_file) < 0) {
        alert(‘上传格式不正确‘);
        $(this).val(‘‘);
        return false;
    }
    var objUrl = getObjectURL(this.files[0]);
    //console.log("objUrl = " + objUrl);
    if (objUrl) {
        // img src=http://www.mamicode.com/objUrl
    }
});

 

h5 js 图片预览并判断