码迷,mamicode.com
首页 > Web开发 > 详细

PHP JS JQ 异步上传并立即显示图片

时间:2014-11-24 22:09:26      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   io   ar   color   os   sp   java   

http://my.oschina.net/zerodeng/blog/313773

 

提交页面:

<! DOCTYPE html>

< html>

< head>

< meta charset ="GB2312" >

< title> Insert title here </title >

< script type ="text/javascript" src= "jquery.js"></ script >

< script type ="text/javascript" >

$(document).ready( function (){

      $( "#i_1" ).load( function(){

             var url = $("#i_1" ).contents().find( "#pic").html();

             if (url != null){

                  $( "#tag_img" ).attr("src" ,url);

            }

      });

});

</ script>

</ head>

< body>

< img id= "tag_img" src = "" />

< form enctype ="multipart/form-data" action= "upload_pic.php" method ="post" target= "upload_target">

     <input type= "file" name ="img" class= "file" value ="" />

     <input type= "submit" name ="uploadimg" value= "上传" />

</ form>

< iframe id= "i_1" name = "upload_target"></ iframe >

</ body>

</ html>

 

 

重点:

1.form中的action="处理图片的有效PHP页面"

2.form中的target="iframe的name属性值"

3.JS中必须要有可以等待iframe加载完后处理iframe返回过来的图片地址。

 

 

处理图片的PHP程序页面:

<?php

$tmp_name = $_FILES[ ‘img‘][ ‘tmp_name‘];

$name = $_FILES[ ‘img‘][ ‘name‘];

move_uploaded_file($tmp_name, ‘./upload/‘.$name);

$img = ‘./upload/‘.$name;

?>

<! DOCTYPE html>

< html>

< head>

< meta name ="viewport" content= "initial-scale=1.0, user-scalable=no" >

< meta http-equiv ="Content-type" content= "text/html;charset:utf-8" >

< script type ="text/javascript" src= "jquery.js"></ script >

</ head>

< body>

< div id= "pic" ><?php echo $img; ?></ div >

</ body>

</ html>

 

 

重点:

1.$_FILE全局超级变量可以接收到POST过来的文件,HTML input的name就是$_FILE[‘name‘]

2.接下来可以做很多处理,如判断是不是图片,图片大小....

3.move_uploaded_file($tmp,$location)函数把图片移动到相应的路径中去,$tmp指的是文件的临时

地址,$location指的是文件移动收的相对路径(包含文件名的路径!)

4.想办法在这个处理页面中找一个地方安放一下处理好的图片。< div id= "pic" ><?php echo $img; ?></ div >

 

 

就这样,我们就可以很轻易的把一个图片异步上传并且立即显示到前台页面中。

PHP JS JQ 异步上传并立即显示图片

标签:style   blog   http   io   ar   color   os   sp   java   

原文地址:http://www.cnblogs.com/mitang/p/4119646.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!