简体   繁体   English

如何使用 html 代码从本地文件中 select 图像

[英]How can I select images from the local file using html codes

Hello everyone I was asked to design a mini page like below using HTML CSS and JS but I am having a problem.大家好,我被要求使用 HTML CSS 和 JS 设计一个如下所示的迷你页面,但我遇到了问题。 When we click on the "+ visual" expression, it is asked to select a visual from the local folder and have a preview, but I did not succeed.当我们点击“+视觉”表达式时,它要求从本地文件夹中查看 select 一个视觉并进行预览,但我没有成功。 I tried to do it with <input type = "file" but Choose File should not appear and it should have a + button.我试图用 <input type = "file" 来做,但选择文件不应该出现,它应该有一个 + 按钮。 I'm so confused please help.我很困惑,请帮忙。

Html Html

<div id="imgbox"> <img id="imgsrch" name="imgsrch" src="images/Button.png" width="120px" onClick="imageReflesh(imgsrch)"></div>

JavaS JavaS

<script language="javascript"> function imageReflesh(imgsrch){document.getElementById("imgsrch").src="images/images.jpg";}</script>

That's the work I'm doing as an alternative, better if I have the chance to convert this.这就是我正在做的替代工作,如果我有机会转换它会更好。 Thank you谢谢

My Work我的工作

There are two parts to this question:这个问题有两个部分:

  • getting it so the user can select an image from their local filestore on clicking the plus sign获取它,以便用户可以在单击加号时从本地文件存储中获取 select 图像
  • displaying the selected image as a preview将所选图像显示为预览

The user can select a file if we use an input element with type="file".如果我们使用 type="file" 的输入元素,用户可以 select 一个文件。 However, it would be preferable if they were taken straight to the selection method in their system rather than see a standard HTML input element.然而,如果他们被直接带到他们系统中的选择方法而不是看到标准的 HTML 输入元素,那将是更可取的。

We can do that by hiding the input element and having a click on the plus sign create a click on the input element.我们可以通过隐藏输入元素并单击加号创建对输入元素的单击来做到这一点。

When a file has been selected we can only see its name, not the full local path (this is for security reasons) so we can't just load the preview image's src attribute.选择文件后,我们只能看到它的名称,而不是完整的本地路径(这是出于安全原因),因此我们不能只加载预览图像的 src 属性。 Instead we have to read the file and put the contents into the preview image.相反,我们必须读取文件并将内容放入预览图像中。

Here is a snippet which does these two things.这是一个完成这两件事的片段。 Of course you will want to format things in the way required by your mini page.当然,您会希望按照您的迷你页面所需的方式来格式化。

 const plusSign = document.querySelector('#plussign'); const getFile = document.querySelector('#getfile'); plusSign.addEventListener('click', function () { getFile.click(); }); getFile.addEventListener('change', function () { preview(getFile.files[0]); }); function preview(file) { const img = document.querySelector('#previewimg'); img.classList.add("obj"); img.file = file; const reader = new FileReader(); reader.onload = (function(image) { return function(e) { image.src = e.target.result; }; })(img); reader.readAsDataURL(file); }
 <div id="plussign" style="font-size: 5em;">+</div> <input type="file" id="getfile" accept="image/*" style="display:none;"> <img id="previewimg"/>

A useful source on using local files can be found here: https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications可以在此处找到有关使用本地文件的有用资源: https://developer.mozilla.org/en-US/docs/Web/API/File/Using_files_from_web_applications

Not sure if I understood your question clearly, but perhaps by using both a label tag and an input[type=file] tag and setting the input's display property to none, like so:不确定我是否清楚地理解了您的问题,但也许通过同时使用 label 标记和 input[type=file] 标记并将输入的显示属性设置为无,如下所示:

<div id="imgbox">
      <label for="file"><img src="./images/Button.png" alt="" /></label>
      <input type="file" name="file" id="file" style="display: none;"/>
</div>

Maybe this variant your will suit:也许这个变种你会适合:

<div id="imgbox"> <img id="imgsrch" name="imgsrch" src="images/Button.png" width="120px" 
onClick="imageReflesh(imgsrch)"></div>

<input style="display: none" id="myInputForm" type="file">

<script language="javascript">
function imageReflesh(imgsrch){document.getElementById("myInputForm").click();}
</script>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 javascript(文件是本地文件)从一个 HTML 文件重定向到另一个文件? - How can I redirect from one HTML file to another using javascript (the files are local)? 如何通过使用HTML / JS / Jquery提供本地文件路径来访问本地文件? - How can I access a local file by giving the local file path using HTML/JS/Jquery? 当我使用javascript从网站保存图像时,如何将图像保存在我想要的本地路径中? - how can i save images in local path where i want when i save the images from website using by javascript? 如何将本地/服务器目录中的所有图像添加到数组,并在另一个文件上使用数组var? - How can i add all images from a local/server directory to array and use the array var on another file? 当用户使用HTML5 File API按下按钮时,如何读取本地文件? - How can I read a local file when the user presses a button using the HTML5 File API ? 如何使用 javascript/jquery 从本地文件夹自动加载多个图像? - How can I auto load multiple images from a local folder using javascript/jquery? 如何使用Fabric.js从本地磁盘向画布添加多个图像? - How can I add multiple images from local disk to canvas using Fabric.js? 我如何让用户使用javascript在其本地计算机上选择一个文件夹(而非文件)? - How can I have a user select a folder (not a file) on their local machine using javascript? JSON本地文件中的HTML选择选项 - HTML select options from json local file 如何在使用javascript添加新元素后更新本地html文件? - How can I update local html file after adding new element using javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM