<script src="https://cdn.bootcdn.net/ajax/libs/jquery/3.6.0/jquery.js"></script>
<input id="img_upload_file1" type="file" />
<img src="" id="img_upload_show" />
<script>
$(function () {
$("#img_upload_file1").change(function () {
var file = this.files[0];
console.log(this.files, 99999);
var reader = new FileReader();
reader.readAsDataURL(file); //调用自带方法进行转换
reader.onload = function (e) {
imgurl = e.target.result; //转化为base64
console.log(imgurl, 787878);
$.ajax({
url: "http://120.92.101.187/index.php/index/uploadimg",
type: "POST",
dataType: "json",
data: { imgurl: imgurl },
success: function (data) {
console.log(data.url, "uuuuu");
if (data.code == 0) {
$("#img_upload_show").attr(
"src",
"http://120.92.101.187/" + data.data.url
);
} else {
alert("上传失败");
}
},
});
};
});
});
</script>
jQuery上传base64格式的图片
最新推荐文章于 2024-05-13 17:58:51 发布