-
移动端上传图片
- <!DOCTYPE html>
- <html>
- <head>
- <title>pc和手机端的图片上传处理</title>
- <meta charset="utf-8" />
- <meta name="viewport" content="width=device-width" />
- </head>
- <body>
- <p>选择:</p>
- <p><input type="file" accept="image/*" id="upload" name="upload"></p>
- <div><button id="surebtn">确定上传</button></div>
- <p>图片预览:</p>
- <p id="preview"></p>
- <script type="text/javascript">
-
- var upload = document.getElementById('upload'),
- preview = document.getElementById('preview'),
- surebtn = document.getElementById('surebtn'),
- imgurl = '';
-
- upload.addEventListener('change',handleFile,false);
- surebtn.addEventListener('click',upLoadFile,false);
-
- function handleFile(){
- window.URL = window.URL || window.webkitURL;
- var sUserAgent= navigator.userAgent.toLowerCase();
- var selected_file = upload.files[0];
-
- if(sUserAgent.match(/android/i) == "android"){
- var img = new Image();
- img.src = window.URL.createObjectURL(selected_file);
- preview.innerHTML = '';
- preview.appendChild(img);
-
-
- var reader = new FileReader();
- reader.onload = function(e) { imgurl = e.target.result; };
- reader.readAsDataURL(selected_file);
-
- }else{
-
- //判断文件类型是否为图片
- var imageType = /image.*/;
-
- if (!selected_file.type.match(imageType)) {
- return false;
- }
-
- var img = document.createElement('img');
- img.file = selected_file;
- preview.innerHTML = '';
- preview.appendChild(img);
-
- img.onload = function(){
- imgurl = img.src;
- }
-
- var reader = new FileReader();
- reader.onload = function(e) { img.src = e.target.result; };
- reader.readAsDataURL(selected_file);
- }
- }
-
- function upLoadFile(){
- var start = imgurl.indexOf(',')+1;
- dataurl = imgurl.substr(start);
-
- var xmlhttp = new XMLHttpRequest();
- xmlhttp.open('post','todo.php',true);
- xmlhttp.setRequestHeader('Content-Type','application/x-www-form-urlencoded;charset=utf-8');
- xmlhttp.setRequestHeader('X_Requested-With','XMLHttpRequest');
- xmlhttp.send('dataurl='+encodeURIComponent(dataurl));
-
- xmlhttp.onreadystatechange=function(){
- if (xmlhttp.readyState==4 && xmlhttp.status==200){
- console.log(xmlhttp.responseText)
- }
- }
- }
-
- </script>
- </body>
- </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 打印时 空格不等于一个汉字问题
使用layui出现Uncaught ReferenceError: layui is not defined
OKR基础知识(三)
-
原文地址:https://www.cnblogs.com/windseek/p/8043885.html
Copyright © 2020-2023
润新知