zoukankan      html  css  js  c++  java
  • 图片上传转base64

    代码:

    <!DOCTYPE html>
    <html>
    <head>
        <meta charset="UTF-8">
        <title>JS 图片转Base64</title>
        <script src="//cdn.bootcss.com/jquery/2.2.0/jquery.min.js"></script>
        <script>
            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())
                    }
                }
            }
            $(function () {
               $("input").change(function () {
                   run(this, function (data) {
                       $('img').attr('src',data);
                       $('textarea').val(data);
                   });
               });
            });
        </script>
    </head>
    <body>
        <input type="file">
        <hr>
        <img style="max-height: 300px; height: 8em; min-8em;">
        <hr>
        <textarea  style="display: block;  100%;height: 30em;"></textarea>
    </body>
    </html>

    参考地址:

    http://www.cnblogs.com/wujingtao/p/5196836.html

  • 相关阅读:
    tushare包使用案例
    Matplotlib模块:绘图和可视化
    pandas使用
    django 表操作
    元数据Meta
    django关系类型字段
    django项目模型字段
    django项目mysite 2
    django安装使用xadmin
    GCC版本中没有GLIBCXX_3.4.15错误
  • 原文地址:https://www.cnblogs.com/wang715100018066/p/7766624.html
Copyright © 2011-2022 走看看