zoukankan      html  css  js  c++  java
  • URL图片预览(createObjectURL)

    1.说明

    1)createObjectURL

      作用:创建url(creates  a URL for the specified object);

      语法:var url = URL.createObjectURL(object,options);

      参数说明:1)object可以包含下列类型:Blob,MSStram,IstaorageItem,MediaCapture,IRandomAccessStreamWithContentType,

           2)options若设置为true,则是oneTimeOnly attribute,doesn't need to be revoked once used,即只是用一次

    2)revokeObjectURL

      作用:将对象鱼url关联(rovokes a URL from  a document and frees the object associated with that URL)

      语法:var retval = URL.revokeObjectURL(url);

    2.例子

    <!DOCTYPE HTML>
    <html>
        <head>
        <meta charset="utf-8">
        <style>
            #preview {
                width: 300px;
                height: 300px;
                overflow: hidden;
            }
            #preview img {
                width: 100%;
                height: 100%;
            }
        </style>
        <script src="../js/jquery-1.9.1.min.js"></script>
    </head>
    <body>
    <form enctype="multipart/form-data" action="" method="post">
        <input type="file" name="imageUpload"/>
        <div id="preview" style=" 300px;height:300px;border:1px solid gray;"></div>
    </form>
    </body>
     <script type="text/javascript">
            $(function(){
                $('[type="file"]').change(function(e){
                    var file = e.target.files[0];
                    preview(file);
                });
            })
            function preview(file){
                var img = new Image();
                url = img.src=URL.createObjectURL(file);
                var $img = $(img);
                img.onload = function(e){
                    URL.revokeObjectURL(url);
                    $('#preview').empty().append($img);
                }
            }
        </script>
    </html>

    效果如下:(点击上传后立刻在页面上显示)

  • 相关阅读:
    编译环境及编译器介绍
    linux下同步window的firefox
    DPDK pdump抓包说明
    linux TCP协议(1)---连接管理与状态机
    Linux用户态数据发送和接收
    DPDK之内存管理
    linux socket系统调用层
    linux网络栈结构
    DPDK mbuf何时释放回内存池?
    虚拟设备之linux网桥
  • 原文地址:https://www.cnblogs.com/wishyouhappy/p/3648423.html
Copyright © 2011-2022 走看看