简体   繁体   English

图像预览无法正常工作

[英]Image preview not working properly

i have this code which shows image before uploading but it is not working properly it shows image if there is no height or width set but if i try to fix height and width to same dimensions it doesn't works properly 我有此代码,该代码在上传之前显示图像,但无法正常工作,如果未设置高度或宽度,则会显示图像,但是如果我尝试将高度和宽度固定为相同尺寸,则无法正常工作

<?php
echo    '<input type="file" id="changeme" name="file" multiple/>
    <div id="output"></div>
    </div>
    <div id="right">
    <script>
    function fileload(e)
    {
        var files=e.target.files || e.dataTransfer.files
        for (var i=0, f; f=files[i]; i++)
        {
            parsefile(f)
        }
    }
    function parsefile(file)
    {
    var reader=new FileReader();
    reader.onload=function (e)
    {
        var output=document.getElementById("output");
        output.innerHTML +="<img src=" + e.target.result + " />";
    }
    reader.readAsDataURL(file); 
    }
    if(window.File && window.FileList && window.FileReader)
    {
        var fileselect=document.getElementById("changeme");
        fileselect.addEventListener("change", fileload);
    }
    </script>';
?>

it works fine this way but when i enter some dimensions like height="50" and width="50" in image tag the image doesn't show up i also tried using css for image but it also doesn't work. 它可以通过这种方式很好地工作,但是当我在图像标签中输入一些height="50"例如height="50"width="50" ,图像没有显示出来,我也尝试过使用CSS作为图像,但它也无法正常工作。 NOTE:- i have to use it inside php for some reasons 注意:-由于某些原因,我必须在php中使用它

I think it's because you don't wrap in quotes the values into <img> tag. 我认为这是因为您没有在<img>标记<img>引号引起来。 Try this: 尝试这个:

output.innerHTML +="<img src=\'" + e.target.result + "\' style=\'width: 50px; height: 50px;\' />";

Note the escaped single quotes to avoid problems in PHP echo 注意转义的单引号,以避免PHP echo问题

output.innerHTML +="<img width=\\'50\\' height=\\'50\\' src=" + e.target.result + " />";

对我来说很好。

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

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