本文实例为大家分享了php实现ajax图片上传的具体代码,供大家参考,具体内容如下
html页面代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
|
<!DOCTYPE html>
< html lang = "en" >
< head >
< meta charset = "UTF-8" >
< title >Title</ title >
< script type = "text/javascript" src = "__PUBLIC__/home/js/jquery-1.11.0.js" ></ script >
</ head >
< body >
< form class = "form-horizontal" role = "form" id = "myForm"
action = "/index/fileupsend" method = "post"
enctype = "multipart/form-data" >
选择文件:< input type = "file" id = "file1" />< br />
< input type = "button" id = "upload" value = "上传" />
< span id = "imgWait" ></ span >
</ form >
< script >
$(function () {
$("#upload").click(function () {
$("#imgWait").html("上传中");
var formData = new FormData();
formData.append("myfile", document.getElementById("file1").files[0]);
$.ajax({
url: "/Home/index/fileupsend",
type: "POST",
data: formData,
/**
*必须false才会自动加上正确的Content-Type
*/
contentType: false,
/**
* 必须false才会避开jQuery对 formdata 的默认处理
* XMLHttpRequest会对 formdata 进行正确的处理
*/
processData: false,
success: function (data) {
if(data){
alert("上传成功!");
}
$("#imgWait").html("上传成功");
},
error: function () {
alert("上传失败!");
$("#imgWait").hide();
}
});
});
});
</ script >
</ body >
</ html >
|
php代码
1
2
3
4
5
|
public function fileupsend(){
$type_pic = $this ->file_upload( '1' , array ( 'jpg' , 'gif' , 'png' , 'jpeg' ), 'filetest' , 'myfile' );
echo $type_pic [ 'img_path' ];
}
|
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:http://www.cnblogs.com/chen1970s/p/7743680.html