<td>网站logo:</td>
<td>
<img src="{$data.web_logo?'/'.$data.web_logo:'/static/Admin/assets/img/add_photo.png'}" alt=""
style="margin-left:20px;border:1px solid #ccc;" width="100px;" height="100px;"
onclick="get_img(this)" id="web_logo">
<input type="file" class="form-control" id="qrcode"
style="display: none;padding-left:20px;" onchange="showimg(this)">
<span style="color: red;margin-left: 10px;">建议尺寸:240*200</span>
</td>
js代码:
var formData = new FormData();
function showimg(e) {
var file = e.files[0];
var type = file.type;
if (!type.match(/.jpg|.jpeg|.png|.bmp/i)) { //判断上传文件格式
$(e).val('');
return swal('温馨提示', '上传的图片格式不正确,请重新选择');
}
formData.set($(e).attr('id'), file);
var img = $(e).prev().get(0);
var reader = new FileReader();
reader.readAsDataURL(file);
reader.onload = function () {
img.src = reader.result;
};
$(e).val('');
}
function get_img(obj) {
$(obj).next().click();
}
$('#btn-add').on('click', function () {
$.ajax({
url:'',
type:'POST',
data:formData,
contentType: false,
processData: false,
dataType:'json',
success:function (data) {
console.log(data);
}
})
});
php代码:
$pic = \Request::file('qrcode');
if ($pic) {
$info_file = $pic->move('./uploads');
if ($info_file) {
$data['web_logo'] = 'uploads/' . $info_file->getSaveName();
}
}
完成