HTML部分(表单部分)
<form id="uploadForm" enctype="multipart/form-data">
<input type="file" id="url1" name="slogo" class="input_h tips" style="width:25%; float:left; height: 44px;" value="" data-toggle="hover" data-place="right" />
<input type="button" class="button bg-blue margin-left" id="image1" value="+ 浏览上传" >
</form>
JavaScript部分(异步提交数据)
$("#image1").click(function(){
var form = new FormData(document.getElementById("uploadForm"));
$.ajax({
url: '{:U("Index/up_file")}',
type: 'POST',
cache: false,
data: form,
processData: false,
contentType: false,
success:function(data){
if(data){
alert("上传成功");
var file_path = "/web_up_file/" + data.slogo.savepath + data.slogo.savename;
$("#logo img").attr("src",file_path);
$("#logo_h").val(file_path);
$("#logo").css("display","block");
$("#url1").val("");
}else{
alert("上传失败");
}
}
});
});
ThinkPHP部分(上传文件到服务器)
function up_file($data){ $upload = new \Think\Upload();// 实例化上传类 $upload->maxSize = 3145728 ;// 设置附件上传大小 $upload->exts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型 $upload->rootPath = './web_up_file/'; // 设置附件上传根目录 $upload->savePath = ''; // 设置附件上传(子)目录 // 上传文件 $info = $upload->upload(); return $info; }