时间:2021-05-26
单张图片上传
展示图:
完整代码:
<!DOCTYPE html><html> <head> <meta charset="utf-8"> <title>ajax上传图片练习</title> <script src="http://libs.baidu.com/jquery/1.9.0/jquery.js"></script> <style type="text/css"> </style> </head> <body> <form id="form"> <label for="exampleInputEmail1">身份证正面</label> <input type="file" id="drawing" name="drawing" onchange="picture(this);" /> <!-- 上传图片的路径 --><input type="hidden" name="" id="front" value="" /> <div id="result"></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=/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'] + '" width="100">'; 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); } // 正面结束 }多个上传
展示:
完整代码:
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); } // 正面结束 // 这是反面 if ( isset( $_FILES['reverse'] ) && $_FILES['reverse']['error'] == 0 ) { $reverse = request()->file('reverse'); $reverse = $reverse->validate( ['ext'=>'jpg,png,gif'] )->move( ROOT_PATH . 'static' . DS . 'upload/mi/img' ); } if ( isset( $reverse ) ) { $contrary = '/static' . DS . 'upload/mi/img/'. $reverse->getSaveName(); $response['whether'] = true; $response['site'] = $contrary; echo json_encode($response); } //银行卡正面 if ( isset( $_FILES['transaction'] ) && $_FILES['transaction']['error'] == 0 ) { $transaction = request()->file('transaction'); $transaction = $transaction->validate( ['ext'=>'jpg,png,gif'] )->move( ROOT_PATH . 'static' . DS . 'upload/mi/img' ); } if ( isset( $transaction ) ) { $stuck = '/static' . DS . 'upload/mi/img/'. $transaction->getSaveName(); $response['whether'] = true; $response['site'] = $stuck; echo json_encode($response); } //银行卡反面 if ( isset( $_FILES['redlining'] ) && $_FILES['redlining']['error'] == 0 ) { $redlining = request()->file('redlining'); $redlining = $redlining->validate( ['ext'=>'jpg,png,gif'] )->move( ROOT_PATH . 'static' . DS . 'upload/mi/img' ); } if ( isset( $redlining ) ) { $other = '/static' . DS . 'upload/mi/img/'. $redlining->getSaveName(); $response['whether'] = true; $response['site'] = $other; echo json_encode($response); } }总结
以上所述是小编给大家介绍的ThinkPHP5 通过ajax插入图片并实时显示,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对网站的支持!
如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!
声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。
本文实例讲述了thinkphp5框架结合plupload实现图片批量上传功能。分享给大家供大家参考,具体如下:在extend目录下新增目录uploader,并新
本文实例讲述了ThinkPHP5+UEditor图片上传到阿里云对象存储OSS。分享给大家供大家参考,具体如下:ThinkPHP5使用富文本UEditor,将富
ThinkPHP5+jQuery+MySql实现投票功能,先给大家展示下效果图,如果大家感觉效果不错,请参考实例代码。效果图:前端代码:基于THINKPHP5实
本文实例讲述了thinkPHP5框架实现基于ajax的分页功能。分享给大家供大家参考,具体如下:最近一个页面的选项卡又牵扯到ajax分页,所以研究了一下tp5的
本文实例讲述了thinkphp5框架扩展redis类方法。分享给大家供大家参考,具体如下:笔者在开发时发现,thinkphp5的自带redis类方法,只有简单的