您的位置:首页 > 移动开发 > Android开发

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

2017-07-27 11:48 561 查看
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();

            }

        }

    });
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐