如何在HTML中使用从文件上传的图像作为背景?

huangapple go评论76阅读模式
英文:

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 &lt;input type=&quot;file&quot;&gt; 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: &lt;input type=&quot;file&quot; name=&quot;selectBackground&quot; id=&quot;selectBackground&quot; accept=&quot;image/png, image/jpeg&quot;&gt;
This is the JavaScript associated with the input
background = selectBackground.value;
document.body.style.background = &quot;#4d3325 url(&#39;&quot; + background + &quot;&#39;)no-repeat center center fixed&quot;;
document.body.style.backgroundSize = &quot;auto 100%&quot;;

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(&#39;input&#39;);
input.addEventListener(&#39;change&#39;, readURL, true);

function readURL() {
  var file = document.getElementById(&quot;input&quot;).files[0];
  var reader = new FileReader();
  reader.onloadend = function() {
    var image = new Image();

    image.src = reader.result;

    image.onload = function() {
      document.getElementById(&quot;myDiv&quot;).style.backgroundImage = &quot;url(&quot; + reader.result + &quot;)&quot;;
    }
  }
  if (file) {
    reader.readAsDataURL(file);
  }
}

<!-- language: lang-html -->

&lt;div id=&quot;myDiv&quot; style=&quot;width:200px; height:200px&quot;&gt;
  &lt;input type=&quot;file&quot; id=&quot;input&quot; class=&quot;custom-file-input&quot; accept=&quot;.png, .jpg, .jpeg, .gif .bmp&quot; /&gt;
&lt;/div&gt;

<!-- 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 &amp;&amp; URL.revokeObjectURL(window.url);// release memory
               const f = e.target.files[0];
               let url = URL.createObjectURL(f);
               window.url = url;
               document.getElementsByClassName(&#39;container&#39;)[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 -->

 &lt;div class=&#39;container&#39;&gt;&lt;/div&gt;
 &lt;input type=&#39;file&#39; accept=&quot;.png, .jpg, .jpeg, .gif .bmp&quot; onchange=&quot;file(event)&quot;&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月15日 11:11:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/76250660.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定