简体   繁体   English

预览图像HTML和JavaScript

[英]Preview image HTML & JavaScript

I am working on an image uploader and I am currently trying to build up an image preview before the upload is done. 我正在使用图片上传器,目前正在尝试在完成上传之前建立图片预览。 I have this HTML code: 我有这个HTML代码:

<input type="file" id="id_imatgeNewPeces"></input><br></br>
<img id="previewing" src="" />

Then I add a listener to the input like this: 然后,将侦听器添加到这样的输入中:

document.getElementById('id_imatgeNewPeces').addEventListener('change', this.handleFileSelect, false);

And finally the function handleFileSelect: 最后是handleFileSelect函数:

handleFileSelect: function(oEvent) {
    var file = oEvent.target.files[0];

    if (!file.type.match('image.*')) {
        sap.m.MessageToast.show("El archivo seleccionado no es una Imagen");
    } else {

        var readerURL = new FileReader();

        readerURL.onload = function() {
            $('#previewing').attr('src', readerURL.result);
            $('#previewing').attr('width', '250px');
            $('#previewing').attr('height', '230px');
            return true;
        };

        readerURL.readAsDataURL(file);
    }
},

I don't know why when I select a file which is not an image it displays the message but when I do select an image when the method onload ends no image is displayed and in addition it seems that the listener has been lost because no further calls are done when if I select another image. 我不知道为什么当我选择一个不是图像的文件时会显示消息,但是当我在方法onload结束时确实选择了一个图像时却没有显示图像,而且似乎监听器已经丢失了,因为没有进一步的显示如果我选择其他图像,则呼叫完成。

The funny thing is that if I place a breakpoint on line '$('#previewing').attr('height', '230px');' 有趣的是,如果我在'$('#previewing').attr('height', '230px');' I can see the image but when I continue it disappears. 我可以看到图像,但是当我继续时它消失了。 In addition when the method onload ends the fileinput "resets" I mean that it says that it has no selected files. 另外,当方法onload结束fileinput“ resets”时,我的意思是说它没有选定的文件。

Besides being a old question, I've found that your code is working as expected: 除了是一个老问题之外,我发现您的代码按预期运行:

I just don't undestand why use a native addEventListener() when you are using for DOM manipulation, being easily replaced by: 我只是不理解为什么在使用进行DOM操作时为什么要使用本机的addEventListener() ,但很容易替换为:

$("#id_imatgeNewPeces").change(handleFileSelect);

 var handleFileSelect = function(oEvent) { var file = oEvent.target.files[0]; if (!file.type.match('image.*')) { console.log("El archivo seleccionado no es una Imagen"); } else { var readerURL = new FileReader(); readerURL.onload = function() { $('#previewing').attr('src', readerURL.result); $('#previewing').attr('width', '250px'); $('#previewing').attr('height', '230px'); return true; }; readerURL.readAsDataURL(file); } }; $("#id_imatgeNewPeces").change(handleFileSelect); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="file" id="id_imatgeNewPeces"> <br/><br/> <img id="previewing" src="" /> 

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM