微信内置浏览器 微信网页上传图片
服务器端post.php代码:
<?php /** * Created by PhpStorm. * User: Yang * Date: 2015/6/29 * Time: 10:52 */ if($_POST){ if($_POST['photo']){ $file_name = time(); $image_content = $_POST['photo']; if (preg_match('/^(data:\s*image\/(\w+);base64,)/', $image_content, $result)){ $type = $result[2]; $path_tou = $_POST['path'].date("Ymd",time())."/"; mkdir("/data/www/".$path_tou); $new_file = $path_tou.$file_name.".".$type; if (file_put_contents("/data/www/".$new_file, base64_decode(str_replace($result[1], '', $image_content)))){ echo $new_file; } } exit; }else{ echo ''; exit; } } ?> |
前端代码:
<input type="file" name="photo0" accept="image/*" id="photo0" > <scrip> $("#photo0").on("change", function(){ var files = !!this.files ? this.files : []; if (!files.length || !window.FileReader) return; if (/^image/.test( files[0].type)){ var reader = new FileReader(); reader.readAsDataURL(files[0]); reader.onloadend = function(){ $("#preimg0").css("display","inline-block"); $("#addimg0").css("display","none"); //$("#preimg0").css("background-image", "url("+this.result+")"); $.ajax({ type:"POST", url:"/upload.php", data:{ "photo":this.result,"path":"/image/order/ol/" }, success:function(data){ //$("#photo").val(data) $("#uploadimg0").val(data); $("#preimg0").css("background-image", "url("+data+")"); } }); } } }); </script> |