• jquery 图片文件转base64 显示


    <!DOCTYPE html>  
    <html>  
    <head>  
        <meta name="viewport" content="width=device-width" />  
        <title>jquery 图片base64</title>  
        <script type='text/javascript' src="@Url.Content("~/Scripts/jquery-1.10.2.js")"></script>  
    </head>  
    <body>  
        <div id="testPhone" class="weui_uploader_input_wrp" style="79px; height:79px;">  
        </div>  
        <input id="testFile" type="file">  
        <hr>  
        <img id="testImg" style="max-height: 300px; height: 8em; min-8em;">  
        <hr>  
        <textarea id="testArea" style="display: block;  100%;height: 30em;"></textarea>  
        <input id="btnTest" type="button" value="提交base" />  
        <script>  
            $("#testPhone").click(function () {  
                $("#testFile").click();  
            });  
      
            $("#testFile").change(function () {  
                run(this, function (data) {  
                    $('#testImg').attr('src', data);  
                    $('#testArea').val(data);  
                });  
            });  
      
            $("#btnTest").click(function () {  
                $.ajax({  
                    url: "/usercenter/testbaseaction",  
                    type: "post",  
                    dataType: "json",  
                    data: {  
                        "content": $("#testArea").val(),  
                    },  
                    async: false,  
                    success: function (result) {  
                        if (result.Code == 200) {  
                            alert(result.Data);  
                        } else {  
                        }  
                    }  
                });  
            });  
      
            function run(input_file, get_data) {  
                /*input_file:文件按钮对象*/  
                /*get_data: 转换成功后执行的方法*/  
                if (typeof (FileReader) === 'undefined') {  
                    alert("抱歉,你的浏览器不支持 FileReader,不能将图片转换为Base64,请使用现代浏览器操作!");  
                } else {  
                    try {  
                        /*图片转Base64 核心代码*/  
                        var file = input_file.files[0];  
                        //这里我们判断下类型如果不是图片就返回 去掉就可以上传任意文件  
                        if (!/image/w+/.test(file.type)) {  
                            alert("请确保文件为图像类型");  
                            return false;  
                        }  
                        var reader = new FileReader();  
                        reader.onload = function () {  
                            get_data(this.result);  
                        }  
                        reader.readAsDataURL(file);  
                    } catch (e) {  
                        alert('图片转Base64出错啦!' + e.toString())  
                    }  
                }  
            }  
        </script>  
    </body>  
    </html>  
    

    jquery 图片base64

     



  • 相关阅读:
    ultraiso:usb-hdd+ v2
    ntfs格式uefi启动u盘
    M.2、U.2谁更好?主流硬盘接口大扫盲
    高清(200万像素)多灯红外防水枪型网络摄像机 DH-IPC-HFW5200-IRA
    佳能 imageclass mf40120
    hotswapagent——热更新代码而无需重启生产环境
    S5700&S5710 产品文档 : 配置
    hp 88a加粉
    传真机色带安装
    Idea下Android的安装与配置 (2019.11更新)
  • 原文地址:https://www.cnblogs.com/interdrp/p/7902506.html
Copyright © 2020-2023  润新知