dongshen7561 2015-07-24 07:50
浏览 35
已采纳

获取图像预览?

I want to add the image upload function and before uploading the file it should show the preview. My code works perfectly fine for the first part as displayed in image posted below but cannot make out why it does not work for the rest of the part. I have added image upload function at the end of each block but it only shows the preview for the first block and not for the rest. enter image description here

Here is the code for the same.

while($row = mysqli_fetch_array($result)){

<form enctype="multipart/form-data" id="form" action="" method="post">
        <input type="file"  id="image" name="img" onChange="readURL(this);"/>
        <img id="blah" src="#" alt="your image" /><br/><br/>
        <input type="button" value="upload" onclick="javascript:uploadInage();" />
    </form>

    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>

<script>

function readURL(input) {

        if (input.files && input.files[0]) {
        var reader = new FileReader();

        reader.onload = function (e) {
            $('#blah')
                .attr('src', e.target.result)
                .width(300)
                .height(340);
        };

        reader.readAsDataURL(input.files[0]);
    }


  }
</script>
}

This is just a part of all the PHP I have.

Can you please help me add preview feature to all the block.

  • 写回答

1条回答 默认 最新

  • dsuikgi9199 2015-07-24 08:14
    关注
    <script>
    
    function readURL(input) {      
            if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                //$('#blah').attr('src', e.target.result).width(300).height(340);
                 $(input).next('img').attr('src', e.target.result).width(300).height(340);
            };
            reader.readAsDataURL(input.files[0]);
        }
    
      }
    </script>
    

    THe commented line was used before. Changed it to the next line and works perfectly.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 关于CMOS电路图的提问
  • ¥15 Verilog hdl密码锁设计
  • ¥15 CreateFile无法获取特殊文件内容
  • ¥35 基于python的有ssl加密传输的socket聊天室
  • ¥15 数码管亮度控制器设计
  • ¥15 kafka客户端跨网段访问,看日志提示连接的还剩内网地址,且访问不通
  • ¥15 关于c语言代码的问题
  • ¥15 c51单片机控制步进电机
  • ¥20 Visual studio无法检测到设备
  • ¥30 vue 页面窗口放大或者缩小元素会变化