在laravel框架中上傳圖片并實(shí)時(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('文件格式錯(cuò)誤,僅支持 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;
}
這里是把上傳,封裝成了一個(gè)方法,添加的時(shí)候直接添加最后的路徑就可以了;
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
}
});
}
/建立一個(gè)可存取到該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í)時(shí)預(yù)覽功能就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
您可能感興趣的文章:- laravel框架 laravel-admin上傳圖片到oss的方法
- Laravel框架實(shí)現(xiàn)文件上傳的方法分析
- Laravel框架實(shí)現(xiàn)的上傳圖片到七牛功能詳解
- Laravel框架文件上傳功能實(shí)現(xiàn)方法示例
- Laravel框架+Blob實(shí)現(xiàn)的多圖上傳功能示例
- Laravel框架實(shí)現(xiàn)點(diǎn)播上傳阿里云功能