ThinkPHP5 通过ajax插入图片并及时显示(完整代码)

ThinkPHP5 通过ajax插入图片并实时显示(完整代码)

完整代码:

<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>ajax上传图片操练</title> <script src="https://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <style type="text/css"> </style> </head> <body> <form> <label for="exampleInputEmail1">身份证正面</label> <input type="file" onchange="picture(this);" /> <!-- 上传图片的路径 --><input type="hidden" value="" /> <div></div> </form> </body> </html> <script> //正面身份证 function picture() { var data = new FormData($('#form')[0]); /* new FormData 的意思 * 获取我们for表单中的所有input的name和value为了更利便传值 * https://segmentfault.com/a/1190000012327982?utm_source=tag-newest */ console.log(data); $.ajax({ url: "http://tp5-shopxo.likeball.top/index.php?s=https://www.jb51.net/api/Mi/measurement", type: 'POST', data: data, dataType: 'JSON', cache: false, processData: false, contentType: false, success: function(data) { // console.log(data); if (data['whether']) { var result = ''; var result1 = ''; result += '<img src="' + 'http://tp5-shopxo.likeball.top/' + data['site'] + '">'; result1 += 'http://tp5-shopxo.likeball.top/' + data['site']; $('#results').html(result); $('#fronts').val(result1); } }, error: function(data) { alert('错误'); } }); } </script>

tp节制器代码

public function measurement() { $response = array(); //这是身份证正面 if ( isset( $_FILES['drawing'] ) && $_FILES['drawing']['error'] == 0 ) { $drawing = request()->file('drawing'); $picture = $drawing->validate( ['ext'=>'jpg,png,gif'] )->move( ROOT_PATH . 'static' . DS . 'upload/mi/img' ); } if ( isset( $picture ) ) { $filePaths = '/static' . DS . 'upload/mi/img/'. $picture->getSaveName(); $response['whether'] = true; $response['site'] = $filePaths; echo json_encode($response); } // 正面竣事 }

多个上传

展示:

ThinkPHP5 通过ajax插入图片并实时显示(完整代码)

完整代码:

<html> <head> <meta charset="UTF-8"> <title>文件上传</title> <style type="text/css"> #front { width: 120px; height: 120px; background-color: #8A6DE9; } #frontage { width: 120px; height: 120px; background-color: #8A6DE9; } #banking { width: 120px; height: 120px; background-color: #8A6DE9; } #house { width: 120px; height: 120px; background-color: #8A6DE9; } </style> <script src="https://libs.baidu.com/jquery/1.9.0/jquery.js"></script> </head> <body> <form> <!-- 1 --> <p>身份证正面:<input type="file" onchange="identity(this)" autocomplete="off" /></p> <input type="text" value="" /> <div></div> <!-- 1 --> <!-- 2 --> <p>身份证后面:<input type="file" onchange="card(this)" autocomplete="off" /></p> <input type="text" value="" /> <div></div> <!-- 2 --> <!-- 3 --> <p>银行卡正面: <input type="file" onchange="obverse(this)" autocomplete="off" /></p> <input type="text" value="" /> <div></div> <!-- 3 --> <!-- 4 --> <p>银行卡后面: <input type="file" onchange="versa(this)" autocomplete="off" /></p> <input type="text" value="" /> <div></div> <!-- 4 --> </form> </body> </html> <!-- 身份证正面 --> <script type="text/javascript"> function identity() { var formData = new FormData(); formData.append("drawing", $('#drawing')[0].files[0]); // console.log(formData); $.ajax({ url: "http://tp5-shopxo.likeball.top/index.php?s=https://www.jb51.net/api/Mi/measurement", type: 'POST', data: formData, dataType: 'JSON', cache: false, processData: false, contentType: false, success: function(data) { console.log(data); if (data['whether'] == true) { var result = ''; var result1 = ''; result += '<img src="' + 'http://tp5-shopxo.likeball.top/' + data['site'] + '">'; result1 += 'http://tp5-shopxo.likeball.top/' + data['site']; $('#front').html(result); $('#fronts').val(result1); } }, error: function(data) { console.log("错误"); } }); } </script> <!-- 身份证后面 --> <script type="text/javascript"> function card() { var formData = new FormData(); formData.append("reverse", $('#reverse')[0].files[0]); // console.log(formData); $.ajax({ url: "http://tp5-shopxo.likeball.top/index.php?s=https://www.jb51.net/api/Mi/measurement", type: 'POST', data: formData, dataType: 'JSON', cache: false, processData: false, contentType: false, success: function(data) { console.log(data); if (data['whether'] == true) { var result = ''; var result1 = ''; result += '<img src="' + 'http://tp5-shopxo.likeball.top/' + data['site'] + '">'; result1 += 'http://tp5-shopxo.likeball.top/' + data['site']; $('#frontage').html(result); $('#frontages').val(result1); } }, error: function(data) { console.log("错误"); } }); } </script> <!-- 银行卡正面 --> <script type="text/javascript"> function obverse() { var formData = new FormData(); formData.append("transaction", $('#transaction')[0].files[0]); // console.log(formData); $.ajax({ url: "http://tp5-shopxo.likeball.top/index.php?s=https://www.jb51.net/api/Mi/measurement", type: 'POST', data: formData, dataType: 'JSON', cache: false, processData: false, contentType: false, success: function(data) { console.log(data); if (data['whether'] == true) { var result = ''; var result1 = ''; result += '<img src="' + 'http://tp5-shopxo.likeball.top/' + data['site'] + '">'; result1 += 'http://tp5-shopxo.likeball.top/' + data['site']; $('#banking').html(result); $('#bankings').val(result1); } }, error: function(data) { console.log("错误"); } }); } </script> <!-- 银行卡后面 --> <script type="text/javascript"> function versa() { var formData = new FormData(); formData.append("redlining", $('#redlining')[0].files[0]); // console.log(formData); $.ajax({ url: "http://tp5-shopxo.likeball.top/index.php?s=https://www.jb51.net/api/Mi/measurement", type: 'POST', data: formData, dataType: 'JSON', cache: false, processData: false, contentType: false, success: function(data) { console.log(data); if (data['whether'] == true) { var result = ''; var result1 = ''; result += '<img src="' + 'http://tp5-shopxo.likeball.top/' + data['site'] + '">'; result1 += 'http://tp5-shopxo.likeball.top/' + data['site']; $('#house').html(result); $('#houses').val(result1); } }, error: function(data) { console.log("错误"); } }); } </script>

tp节制器中

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/wsdfwp.html