主頁 > 知識(shí)庫 > ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)

ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)

熱門標(biāo)簽:廣西智能外呼系統(tǒng)多少錢 福建微碼電話機(jī)器人 大學(xué)校門地圖標(biāo)注 荊州智能電銷機(jī)器人 銷售電銷機(jī)器人詐騙 提高電話機(jī)器人接通率 地圖標(biāo)注與公司業(yè)務(wù)關(guān)系 外呼系統(tǒng)api對(duì)接 平?jīng)龈叩碌貓D標(biāo)注商戶要收費(fèi)嗎

單張圖片上傳

展示圖:

完整代碼:

!DOCTYPE html>
html>
 head>
 meta charset="utf-8">
 title>ajax上傳圖片練習(xí)/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('錯(cuò)誤');
 }
 });
 }
/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); 
  }
  // 正面結(jié)束
 }

多個(gè)上傳

展示:

完整代碼:

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="http://libs.baidu.com/jquery/1.9.0/jquery.js">/script>
 /head>
 body>
 form id="uploadForm">
 !-- 1 -->
 p>身份證正面:input type="file" name="drawing" id="drawing" onchange="identity(this)" autocomplete="off" />/p>
 input type="text" name="" id="fronts" value="" />
 div id="front">/div>
 !-- 1 -->
 !-- 2 -->
 p>身份證反面:input type="file" name="reverse" id="reverse" onchange="card(this)" autocomplete="off" />/p>
 input type="text" name="" id="frontages" value="" />
 div id="frontage">/div>
 !-- 2 -->
 !-- 3 -->
 p>銀行卡正面: input type="file" name="transaction" id="transaction" onchange="obverse(this)" autocomplete="off" />/p>
 input type="text" name="" id="bankings" value="" />
 div id="banking">/div>
 !-- 3 -->
 !-- 4 -->
 p>銀行卡反面: input type="file" name="redlining" id="redlining" onchange="versa(this)" autocomplete="off" />/p>
 input type="text" name="" id="houses" value="" />
 div id="house">/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=/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'] + '" width="100">';
  result1 += 'http://tp5-shopxo.likeball.top/' + data['site'];
  $('#front').html(result);
  $('#fronts').val(result1);
 }
 },
 error: function(data) {
 console.log("錯(cuò)誤");
 }
 });
 }
/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=/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'] + '" width="100">';
  result1 += 'http://tp5-shopxo.likeball.top/' + data['site'];
  $('#frontage').html(result);
  $('#frontages').val(result1);
 }
 },
 error: function(data) {
 console.log("錯(cuò)誤");
 }
 });
 }
/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=/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'] + '" width="100">';
  result1 += 'http://tp5-shopxo.likeball.top/' + data['site'];
  $('#banking').html(result);
  $('#bankings').val(result1);
 }
 },
 error: function(data) {
 console.log("錯(cuò)誤");
 }
 });
 }
/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=/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'] + '" width="100">';
  result1 += 'http://tp5-shopxo.likeball.top/' + data['site'];
  $('#house').html(result);
  $('#houses').val(result1);
 }
 },
 error: function(data) {
 console.log("錯(cuò)誤");
 }
 });
 }
/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); 
  }
  // 正面結(jié)束
  // 這是反面
  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); 
  }
 }

總結(jié)

以上所述是小編給大家介紹的ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

您可能感興趣的文章:
  • thinkphp5 + ajax 使用formdata提交數(shù)據(jù)(包括文件上傳) 后臺(tái)返回json完整實(shí)例
  • TP5(thinkPHP5)框架使用ajax實(shí)現(xiàn)與后臺(tái)數(shù)據(jù)交互的方法小結(jié)
  • ThinkPHP5.1+Ajax實(shí)現(xiàn)的無刷新分頁功能示例
  • Thinkphp5框架ajax接口實(shí)現(xiàn)方法分析
  • thinkPHP+mysql+ajax實(shí)現(xiàn)的仿百度一下即時(shí)搜索效果詳解
  • ThinkPHP框架結(jié)合Ajax實(shí)現(xiàn)用戶名校驗(yàn)功能示例
  • thinkPHP利用ajax異步上傳圖片并顯示、刪除的示例
  • TP5(thinkPHP5)框架基于ajax與后臺(tái)數(shù)據(jù)交互操作簡(jiǎn)單示例
  • ThinkPHP 5 AJAX跨域請(qǐng)求頭設(shè)置實(shí)現(xiàn)過程解析

標(biāo)簽:樂山 黔東 婁底 邯鄲 海南 德陽 衡陽 內(nèi)江

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)》,本文關(guān)鍵詞  ThinkPHP5,通過,ajax,插入,圖片,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 下面列出與本文章《ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)》相關(guān)的同類信息!
  • 本頁收集關(guān)于ThinkPHP5 通過ajax插入圖片并實(shí)時(shí)顯示(完整代碼)的相關(guān)信息資訊供網(wǎng)民參考!
  • 推薦文章