tp5表單的圖片和文字上傳
阿新 • • 發佈:2018-10-31
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()]; } }