廢話不多說,直接貼代碼
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
}
作為筆記用,先貼上后面再解釋
用法
html:
<label class="btn btn-default"><span>選擇文件</span> <input type="file" class="uploadimg" name="img" /></label>
<div class="imgBox"><img src="" class="imgPrvew"></div>
$('.uploadimg').on('change', function() {
var imgUrl = getObjectURL(this.files[0]);
$(".imgPrvew").attr('src', imgUrl)
});
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
}
先這樣....