dongshao9106 2016-03-17 09:48 采纳率: 100%
浏览 19
已采纳

图像预览无法正常工作

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. NOTE:- i have to use it inside php for some reasons

  • 写回答

2条回答 默认 最新

  • douque8861 2016-03-17 09:56
    关注

    I think it's because you don't wrap in quotes the values into <img> tag. 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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?
  • ¥30 求解达问题(有红包)