英文:
How can I use an image from a file upload in HTML as a background?
问题
我正在尝试使用 <input type="file">
允许用户上传一张将用作背景的图像。我不知道是否需要获取文件的完整路径。
这是我尝试使用的输入: <input type="file" name="selectBackground" id="selectBackground" accept="image/png, image/jpeg">
这是与输入相关联的JavaScript代码:
background = selectBackground.value;
document.body.style.background = "#4d3325 url('" + background + "')no-repeat center center fixed";
document.body.style.backgroundSize = "auto 100%";
背景根本不会改变,当我尝试将其显示为普通图像时,它只显示一个小图像图标。
英文:
I'm trying to use <input type="file">
to allow a user to upload an image that will be used as a background. I don't know if I need to get the full path of the file or not.
This is the input I'm trying to use: <input type="file" name="selectBackground" id="selectBackground" accept="image/png, image/jpeg">
This is the JavaScript associated with the input
background = selectBackground.value;
document.body.style.background = "#4d3325 url('" + background + "')no-repeat center center fixed";
document.body.style.backgroundSize = "auto 100%";
The background doesn't change at all and when I try to display it as a regular image, it just shows a small image icon.
答案1
得分: 0
以下是您提供的代码的翻译部分:
var input = document.getElementById('input');
input.addEventListener('change', readURL, true);
function readURL() {
var file = document.getElementById("input").files[0];
var reader = new FileReader();
reader.onloadend = function() {
var image = new Image();
image.src = reader.result;
image.onload = function() {
document.getElementById("myDiv").style.backgroundImage = "url(" + reader.result + ")";
}
}
if (file) {
reader.readAsDataURL(file);
}
}
<div id="myDiv" style="width:200px; height:200px">
<input type="file" id="input" class="custom-file-input" accept=".png, .jpg, .jpeg, .gif .bmp" />
</div>
请注意,这是您提供的代码的翻译版本,不包括任何其他内容。
英文:
Hi friend check if this solution attends your needs:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
var input = document.getElementById('input');
input.addEventListener('change', readURL, true);
function readURL() {
var file = document.getElementById("input").files[0];
var reader = new FileReader();
reader.onloadend = function() {
var image = new Image();
image.src = reader.result;
image.onload = function() {
document.getElementById("myDiv").style.backgroundImage = "url(" + reader.result + ")";
}
}
if (file) {
reader.readAsDataURL(file);
}
}
<!-- language: lang-html -->
<div id="myDiv" style="width:200px; height:200px">
<input type="file" id="input" class="custom-file-input" accept=".png, .jpg, .jpeg, .gif .bmp" />
</div>
<!-- end snippet -->
答案2
得分: 0
使用URL.createObjectURL()
通过使用这个方法,上传的图像文件会被转换成对象URL。
最后,当我们更改其他图像时,应该使用URL.revokeObjectURL()
来从内存中删除旧的URL,以便更好地管理内存。
function file(e){
window.url && URL.revokeObjectURL(window.url);// 释放内存
const f = e.target.files[0];
let url = URL.createObjectURL(f);
window.url = url;
document.getElementsByClassName('container')[0].style.backgroundImage = `url(${url})`;
}
.container{
width: 100px;
height: 100px;
border: 1px solid lightgrey;
margin: 10px;
background-size: contain;
background-repeat: no-repeat;
}
<div class='container'></div>
<input type='file' accept=".png, .jpg, .jpeg, .gif, .bmp" onchange="file(event)">
英文:
Use URL.createObjectURL()
By using this, the uploaded image file is converted in to object url.
Finally when we change the other image we should remove the old url from memory for better memory management by using URL.revokeObjectURL()
.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
function file(e){
window.url && URL.revokeObjectURL(window.url);// release memory
const f = e.target.files[0];
let url = URL.createObjectURL(f);
window.url = url;
document.getElementsByClassName('container')[0].style.backgroundImage = `url(${url})`;
}
<!-- language: lang-css -->
.container{
width: 100px;
height: 100px;
border: 1px solid lightgrey;
margin: 10px;
background-size: contain;
background-repeat: no-repeat;
}
<!-- language: lang-html -->
<div class='container'></div>
<input type='file' accept=".png, .jpg, .jpeg, .gif .bmp" onchange="file(event)">
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论