3 回答

TA貢獻1842條經驗 獲得超13個贊
代碼中需要一些修復,必須讀取文件并創(chuàng)建 URL,這里是代碼的工作片段。
const uploadPictureButton = document.querySelector(".photo-upload");
uploadPictureButton.addEventListener('change', function () {
displayPicture(this);
});
function displayPicture(input) {
if (input.files && input.files[0]) {
var reader = new FileReader();
reader.onload = function (e) {
document.getElementById('the-picture').setAttribute('src', e.target.result);
};
reader.readAsDataURL(input.files[0]);
}
}
<input
id="myImage"
class="photo-upload"
type="file"
accept="image/*, image/jpeg">
<img id="the-picture" width="200" />

TA貢獻1772條經驗 獲得超5個贊
試試這個,它對我有用
<img height="100" width="100" alt="avatar" id="imgPreview> <input type="file" onchange="document.querySelector("#imgPreview").src = window.URL.createObjectURL(this.files[0])">
添加回答
舉報