[英]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
谢谢
There are two parts to this question:这个问题有两个部分:
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.