美文网首页
图片预览自适应

图片预览自适应

作者: 小慕先森 | 来源:发表于2017-08-16 22:48 被阅读0次

    一、表单单图片上传

     <form action="#" class="form-horizontal tasi-form" id="formpic" enctype="multipart/form-data">
                            
    
            <div  style="width:400px;height: 400px; position: relative;overflow: hidden;margin-right:20px;padding: 0;border:1px solid #CCCCCC;text-align:center;vertical-align:middle;line-height: 400px">
       
            <img src="" alt=""  class="img-thumbnail" id="show_img" style="vertical-align:middle;" onload="resizeimg(this,400,400);">
    
             <input type="file" name="file" id="oFile"  style=" position:absolute;top:0;left:0;width: 100%; height: 100%; cursor: pointer; opacity: 0;"/>
                  </div>
                             
              
                                   
                               
     </form>
     
      
    
      <script>
      function resizeimg(obj,maxW,maxH)
      {
               var imgW=obj.width;
               var imgH=obj.height;
    
               if(imgW>maxW||imgH>maxH)
               {       
                        var ratioA=imgW/maxW;
                        var ratioB=imgH/maxH;               
                        if(ratioA>ratioB)
                        {
                                 imgH=maxW*(imgH/imgW);
                                 imgW=maxW;
               
                        }
                        else
                        {
                                 imgW=maxH*(imgW/imgH);
                                 imgH=maxH;
    
                        }  
                        obj.width=imgW;
                        obj.height=imgH;
               }
    
           
      }
      $(function(){
     
        $("#oFile").change(function(){
            var fileObj = this.files[0] ; 
            var reader = new FileReader();
              reader.readAsDataURL(fileObj);
              reader.onload = function(f) {
                var show_img = $("#show_img");
            
                show_img.removeAttr('width');
                show_img.removeAttr('height');
                show_img.attr("src", this.result);
    
              }
    
            $.ajax({
              url: ,
              type: 'POST',
              cache: false,
              data: new FormData($('#formpic')[0]),
              dataType: 'json',
              processData: false,
              contentType: false,
              success:function(data){
                  console.og(data);
                  
                },
              error:function(error){
                  
              }
            });
        });
    

    二、多图片

    $("#compare").click(function(){
         var form = new FormData();
         form.append('file_img[]', document.getElementById('oFile').files[0]);
         form.append('file_img[]', document.getElementById('oFile1').files[0]);
         if(window.XMLHttpRequest){
               var xhr=new XMLHttpRequest();
         }else{
               var xhr=new ActiveXObject('Microsoft.XMLHTTP');
         }
         xhr.open('POST', "<?= Url::to(['face/upload']) ?>",true);
         xhr.send(form);
         xhr.onreadystatechange = function () {
             if (xhr.readyState == 4){
                  if (xhr.status>=200 && xhr.status<300 || xhr.status==304) {
                                   console.log('发送'); //仅示例
                                   var data = JSON.parse(xhr.responseText);
        
                      }
               }
        };
       });
    

    相关文章

      网友评论

          本文标题:图片预览自适应

          本文链接:https://www.haomeiwen.com/subject/wnvwrxtx.html