!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 的默認(rèn)處理
* XMLHttpRequest會對 formdata 進(jìn)行正確的處理
*/
processData: false,
success: function (data) {
if(data){
alert("上傳成功!");
}
$("#imgWait").html("上傳成功");
},
error: function () {
alert("上傳失??!");
$("#imgWait").hide();
}
});
});
});
/script>
/body>
/html>
public function fileupsend(){
$type_pic = $this->file_upload('1',array('jpg', 'gif', 'png', 'jpeg'),'filetest','myfile');
echo $type_pic['img_path'];
}