欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

laravel框架上傳圖片實(shí)現(xiàn)實(shí)時預(yù)覽功能

 更新時間:2019年10月14日 09:33:22   作者:QDX_1209_BJ  
今天小編就為大家分享一篇laravel框架上傳圖片實(shí)現(xiàn)實(shí)時預(yù)覽功能,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧

在laravel框架中上傳圖片并實(shí)時預(yù)覽,其實(shí)并沒有那么難,下面給大家展示一下;

HTML代碼:

<img class="pic house-a" οnclick="houseImgOne(this)" name="house_img_one" id="house_img_one" src="">
<input type="file" name="house_img_one" id="house_img_one1" multiple="multiple" style="display:none;">


controller代碼:

 public function upload($photo)
  {
    $file_ex = $photo->getClientOriginalExtension();
    if (!in_array($file_ex, array('jpg', 'gif', 'png', 'jpeg'))) {
      echo "<script>alert('文件格式錯誤,僅支持 jpg ,gif,png,jpeg');location.href='/apply'</script>";
    }
    $newname = date('Ymdhis') . rand(1, 999) . "." . $file_ex;
    $savepath = config('constants.img_uf') .'Uploads/Apply/';
    $path = $photo->move($savepath, $newname);
    $filepath = "UF/Uploads/Apply/" . $newname;
    return $filepath;
  }

這里是把上傳,封裝成了一個方法,添加的時候直接添加最后的路徑就可以了;

js代碼:

var _btnId = '';
function houseImgOne(_this){
  _btnId = $(_this).attr('id');
  $('#house_img_one1').click();
  $("#house_img_one1").change(function () {
    var objUrl = getObjectURL(this.files[0]); //獲取圖片的路徑,該路徑不是圖片在本地的路徑
    if (objUrl) {
      $("#" + _btnId).attr("src", objUrl); //將圖片路徑存入src中,顯示出圖片ai
    }
  });
}
/建立一個可存取到該file的url
function getObjectURL(file) {
  var url = null;
  if (window.createObjectURL != undefined) { 
    url = window.createObjectURL(file);
  } else if (window.URL != undefined) { 
    url = window.URL.createObjectURL(file);
  } else if (window.webkitURL != undefined) { 
    url = window.webkitURL.createObjectURL(file);
  }
  return url;
}

效果圖片:

以上這篇laravel框架上傳圖片實(shí)現(xiàn)實(shí)時預(yù)覽功能就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評論