input 上传图片显示预览、调用摄像头,ios和Android的兼容性解决

来源:互联网 发布:mx anywhere2 软件 编辑:程序博客网 时间:2024/05/22 09:01

html代码:

  <input id="uploadImage1" type="file" name="" class="fimg1" accept="image/*" capture="camera" onchange=""/></label>

                 

      注意:IOS和Android有兼容性问题,IOS只能拍照,不能从相册选择

解决:

$(function () {
//解决上传图片时capture="camera"在安卓与IOS的兼容性问题(在IOS只能拍照,不能选相册)
var ua = navigator.userAgent.toLowerCase();//获取浏览器的userAgent,并转化为小写——注:userAgent是用户可以修改的
var isIos = (ua.indexOf('iphone') != -1) || (ua.indexOf('ipad') != -1);//判断是否是苹果手机,是则是true
if (isIos) {
$("input:file").removeAttr("capture");
};
})


JS代码:

$("#uploadImage1").on("change", function(){
        // Get a reference to the fileList
        var files = !!this.files ? this.files : [];


        // If no files were selected, or no FileReader support, return
        if (!files.length || !window.FileReader) return;


        // Only proceed if the selected file is an image
        if (/^image/.test( files[0].type)){


            // Create a new instance of the FileReader
            var reader = new FileReader();


            // Read the local file as a DataURL
            reader.readAsDataURL(files[0]);


            // When loaded, set image data as background of div
            reader.onloadend = function(){


                $("#uploadPreview1").css("background-image", "url("+this.result+")");


               // loginControl2();
            }


        }


    });