jQuery文件上传

来源:互联网 发布:steam更新网络连接 编辑:程序博客网 时间:2024/04/29 02:47

HTML

应用jQuery.js   和   jQuery.from.js

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>无刷新文件上传</title>
    <script type="text/javascript" src="jquery.js"></script>
    <script type="text/javascript" src="jquery.form.js"></script>
</head>
<body>
    <div style="width:600px">
        <form id="imageform" method="post" enctype="multipart/form-data" action='change.php'>
        上传 <input type="file" name="photoimg" id="photoimg" />
        </form>
        <div id='preview'></div>
    </div>
</body>
</html>
<!-- form类 -->
<script type="text/javascript">
 $(document).ready(function(){
    $('#photoimg').change('change.php',function(){
        $("#preview").html('');
        $("#preview").html('<img src="" alt="Uploading...." title="asd"/>');
        $("#imageform").ajaxForm({
             target:'#preview'
        }).submit();
     });
});
</script>

controller

 <?php
    header("content-type:text/html;charset=utf-8");
    $file = $_FILES['photoimg'];
    $type=substr($file['name'],strrpos($file['name'],'.')+1);
    $size=$file['size'];
    $valid_formats = array("jpg", "png", "gif", "bmp");
    if(!in_array($type,$valid_formats)){
        echo "图片格式不符合要求";exit;
    }
    /*if($size>1024*1){
        echo "<script>alert('上传失败')</script>";exit;
        echo "图片不能大于3Mb";exit;
    }*/
    $filename = rand(111,999).time().'.'.$type;
    $path = "./video/".$filename;
    move_uploaded_file($file['tmp_name'],$path);
    echo "<img src='".$path."' class='preview' width='100'>";  
?>

0 0
原创粉丝点击