• 移动端上传图片


    1. <!DOCTYPE html>  
    2. <html>  
    3. <head>  
    4.     <title>pc和手机端的图片上传处理</title>  
    5.     <meta charset="utf-8" />  
    6.     <meta name="viewport" content="width=device-width" />  
    7. </head>  
    8. <body>  
    9.     <p>选择:</p>  
    10.     <p><input type="file" accept="image/*" id="upload" name="upload"></p>  
    11.     <div><button id="surebtn">确定上传</button></div>  
    12.     <p>图片预览:</p>  
    13.     <id="preview"></p>  
    14.     <script type="text/javascript">  
    15.   
    16.         var upload = document.getElementById('upload'),  
    17.             preview = document.getElementById('preview'),  
    18.             surebtn = document.getElementById('surebtn'),  
    19.             imgurl = '';  
    20.   
    21.         upload.addEventListener('change',handleFile,false);  
    22.         surebtn.addEventListener('click',upLoadFile,false);  
    23.   
    24.         function handleFile(){  
    25.             window.URL = window.URL || window.webkitURL;  
    26.             var sUserAgent= navigator.userAgent.toLowerCase();  
    27.             var selected_file = upload.files[0];  
    28.   
    29.             if(sUserAgent.match(/android/i) == "android"){  
    30.                 var img = new Image();  
    31.                 img.src = window.URL.createObjectURL(selected_file);  
    32.                 preview.innerHTML = '';  
    33.                 preview.appendChild(img);  
    34.   
    35.   
    36.                 var reader = new FileReader();  
    37.                 reader.onload = function(e) { imgurl = e.target.result; };  
    38.                 reader.readAsDataURL(selected_file);  
    39.   
    40.             }else{  
    41.   
    42.                 //判断文件类型是否为图片  
    43.                  var imageType = /image.*/;  
    44.           
    45.                 if (!selected_file.type.match(imageType)) {  
    46.                   return false;  
    47.                 }  
    48.   
    49.                 var img = document.createElement('img');  
    50.                 img.file = selected_file;  
    51.                 preview.innerHTML = '';  
    52.                 preview.appendChild(img);  
    53.   
    54.                 img.onload = function(){  
    55.                     imgurl = img.src;  
    56.                 }  
    57.   
    58.                 var reader = new FileReader();  
    59.                 reader.onload = function(e) { img.src = e.target.result; };  
    60.                 reader.readAsDataURL(selected_file);  
    61.             }  
    62.         }  
    63.   
    64.         function upLoadFile(){  
    65.             var start = imgurl.indexOf(',')+1;  
    66.             dataurl = imgurl.substr(start);  
    67.   
    68.             var xmlhttp = new XMLHttpRequest();  
    69.             xmlhttp.open('post','todo.php',true);  
    70.             xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');  
    71.             xmlhttp.setRequestHeader('X_Requested-With','XMLHttpRequest');  
    72.             xmlhttp.send('dataurl='+encodeURIComponent(dataurl));  
    73.   
    74.             xmlhttp.onreadystatechange=function(){  
    75.                 if (xmlhttp.readyState==4 && xmlhttp.status==200){  
    76.                     console.log(xmlhttp.responseText)  
    77.                 }  
    78.             }  
    79.         }  
    80.   
    81.     </script>  
    82. </body>  
    83. </html>  
    学而不思则罔,思而不结则殆,结而不看,一事无成
  • 相关阅读:
    深圳的2022
    Erlang port communicate with python
    Google v8实现类似nodejs的import加载模块
    手撕某解析网站,获取真实播放地址(保姆级讲解)(转)
    存储过程中组建集合
    svn cleanup 系统找不到指定路径_SVN:cleanup failed to process the following paths 错误
    安装Android Studio时,没有sdk列表
    html 打印时 &nbsp; &nbsp; 空格不等于一个汉字问题
    使用layui出现Uncaught ReferenceError: layui is not defined
    OKR基础知识(三)
  • 原文地址:https://www.cnblogs.com/windseek/p/8043885.html
Copyright © 2020-2023  润新知