Html:
<form enctype="multipart/form-data" >
<label>标题:</label>
<input type="text" class="input-text" value="" id="title" required>
<label>图片上传:</label>
<input type="file" id="img" >
<button id="submit" type="submit"> 提交</button>
</form>
JS:
$('#submit').click(function(){
var from = new FormData();
var title=$('#title').val();
var img=$('#img').get(0).files[0];
from.append("file", img);
from.append("title",title);
$.ajax({
type: 'post',
url:"{:url('admin/map/upload')}",
data:from,
dataType:'json',
contentType: false,
processData: false,
success: function (data) {
if(data.status==1){
alert(data.message);
}else{
alert(data.message);
}
}
});
return false;
})
TP5:
public function upload(){
//
$file=request()->file('file');
$title=request()->param('title');
$info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
if($info){
$mapData= new MapModel;
$mapData->img='/uploads/'.$info->getSaveName();
$mapData->title=$title;
$result=$mapData->save();
//上传成功信息
return ['status'=>1,'message'=>"上传成功"];
}else{
// 上传失败获取错误信息
return ['status'=>0,'message'=> $file->getError()];
}
}