如何在上传前后预览图片?

时间:2013-04-25 06:08:56

标签: php javascript jquery html5

我要在表单中预览图片或照片,但它不起作用,HTML代码如下所示:

<form action="" method="post" enctype="multipart/form-data" name="personal_image" id="newHotnessForm">
    <p><label for="image">Upload Image:</label>
    <input type="file" id="imageUpload"/></p>
    <p><button type="submit" class="button">Save</button></p>
        <div id="preview">
            <img width="160px" height="120px" src="profile pic.jpg" id="thumb" />
        </div>
    </form>

并在下面加入了JS代码/脚本:

<script type="text/jaavascript">
$(document).ready(function(){
    var thumb=$('#thumb');
    new AjaxUpload('imageUpload',{
    action:$('newHotnessForm').attr('action'),
    name:'image',
    onSubmit:function(file,extension){
        $('#preview').addClass('loading');
    },
    onComplete:function(file,response){
        thumb.load(function(){
            $('#preview').removeClass('loading');
            thumb.unbind();
        });
        thumb.attr('src',response);
    }
    });
});

我的表格上有两个主要问题:
1.为什么图像或图片的预览不起作用?
2。如何在单击保存按钮时粘贴表单中的照片,它将链接到我创建的另一个PHP或PHP页面?

5 个答案:

答案 0 :(得分:69)

试试这个:(预览)

<script type="text/javascript">
        function readURL(input) {
            if (input.files && input.files[0]) {
                var reader = new FileReader();

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

                reader.readAsDataURL(input.files[0]);
            }
        }
    </script>

<body>
    <form id="form1" runat="server">
        <input type='file' onchange="readURL(this);" />
        <img id="blah" src="#" alt="your image" />
    </form>
</body>

工作演示here>

答案 1 :(得分:6)

meVeekay的答案很好,只是通过做两件事来让它更加即兴。

  1. 检查浏览器是否支持HTML5 FileReader()。

  2. 通过检查其扩展名,仅允许上传图片文件。

  3. HTML:

    <div id="wrapper">
        <input id="fileUpload" type="file" />
        <br />
        <div id="image-holder"></div>
    </div> 
    

    jQuery:

    $("#fileUpload").on('change', function () {
    
        var imgPath = $(this)[0].value;
        var extn = imgPath.substring(imgPath.lastIndexOf('.') + 1).toLowerCase();
    
        if (extn == "gif" || extn == "png" || extn == "jpg" || extn == "jpeg") {
            if (typeof (FileReader) != "undefined") {
    
                var image_holder = $("#image-holder");
                image_holder.empty();
    
                var reader = new FileReader();
                reader.onload = function (e) {
                    $("<img />", {
                        "src": e.target.result,
                            "class": "thumb-image"
                    }).appendTo(image_holder);
    
                }
                image_holder.show();
                reader.readAsDataURL($(this)[0].files[0]);
            } else {
                alert("This browser does not support FileReader.");
            }
        } else {
            alert("Pls select only images");
        }
    });
    

    详细了解FileReader()

    选中 Article : Using FileReader() preview image before uploading.

答案 2 :(得分:2)

                    #######################
                    ###  the img page   ###
                    #######################


<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script>
  <script src="http://malsup.github.com/jquery.form.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#f').live('change' ,function(){
           $('#fo').ajaxForm({target: '#d'}).submit();
                      });


    });
// });
</script>
<form id="fo" name="fo" action="nextimg.php" enctype="multipart/form-data" method="post">
 <input type="file" name="f" id="f" value="start upload" />
<input type="submit" name="sub" value="upload"/>
</form>
<div id="d"></div>





                    #############################
                    ###    the nextimg page   ###
                    #############################




     <?php

     $name=$_FILES['f']['name']; 
     $tmp=$_FILES['f']['tmp_name'];
     $new=time().$name;
     $new="upload/".$new;
     move_uploaded_file($tmp,$new);
     if($_FILES['f']['error']==0)
      {

     ?>
     <h1>PREVIEW</h1><br /><img src="<?php echo $new;?>" width="100" height="100"/>
     <?php
     }
     ?>

答案 3 :(得分:2)

在输入 type = file 时添加事件 onchange =“ preview()”

对于功能preview(),请输入:

thumb.src=URL.createObjectURL(event.target.files[0]);

实时示例:

function preview() {
   thumb.src=URL.createObjectURL(event.target.files[0]);
}
<form>
    <input type="file" onchange="preview()">
    <img id="thumb" src="" width="150px"/>
</form>

答案 4 :(得分:0)

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

    reader.onload = function(e) {
      $('#ImdID').attr('src', e.target.result);
    };

    reader.readAsDataURL(input.files[0]);
  }
}
img {
  max-width: 180px;
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type='file' onchange="readURL(this);" />
<img id="ImdID" src="" alt="Image" />

相关问题