HTML文件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8">
<title>Document</title>
<link rel="stylesheet" type="text/css" href="__PUBLIC__/Style/weireset.css" />
<script type="text/javascript" src="__PUBLIC__/Style/jquery172.js"></script>
<script type="text/javascript" src="__PUBLIC__/Style/jquery.form.js"></script>
</head>
<body>
<script type="text/javascript">
$(function(){
$("#t_file").change(function(){
if($("#t_file").val() == "") return;
$("#t_form").ajaxSubmit({
dataType: 'json',
beforeSend: function() {
$('#t_stat').empty();
$('#t_stat').html("上传中...");
},
success: function(data) {
if(data.state == 'SUCCESS'){
$('#t_stat').html(""+ data.title +" 上传成功("+data.size+"k)");
var timg = data.turl;//原图
$('#t_img').attr("src",timg);
$("#t_dir").val(timg);
}else {
$('#t_dir').html(data.state);
}
},
error:function(xhr){
$('#t_stat').html("上传数百");
$('#t_dir').html(xhr);
}
});
});
});
</script>
<div class="bordered" style="margin:100px;padding:10px;">
<form id="upload1" action="{:U('Manage/Test/submit')}" method="post">
<input type="text" name="title" value="这里是标题" /><br/>
<form id='t_form' action='{:U('Manage/Test/ajaxUpload')}' method='post' enctype='multipart/form-data'>
<input id="t_file" type="file" name="imgfile" class="mtp10" /><br/>
</form>
<input id="t_dir" type="text" name="thumb" size="60" value="" /><br/>
<span id="t_stat">上传状态</span><br/>
<input type="submit" value="SUBMIT" class="mtp10" /><br/>
</form>
<img id="t_img" src="/xyh/Uploads/Thumbs/20140701/53b250c77f0ac.jpg" alt="111" width="240" height="120" class="mtp10" />
</div>
</body>
</html>
PHP文件
public function ajaxUpload(){
header("Content-Type:text/html; charset=utf-8");
$config=C('uploadImg');
$upload=new \Think\Upload($config);
$info=$upload->uploadOne($_FILES['imgfile']);
if(!$info){
$this->error($upload->getError());
}else{
$info['savepath'] = __ROOT__.ltrim($info['savepath'],'.');
$picture_turl=$info['savepath'].$info['savename'];
echo json_encode(
array(
'url' => $picture_url,
'turl' => $picture_turl,
'title' => $info['name'],
'original' => $info['name'],
'state' => 'SUCCESS',
'size' => round($info['size']/1024,2)
));
}
}
PS:适用于THINKPHP3.2.2