<h3 style="Box-sizing: border-Box; font-weight: 500; line-height: 1.1; color: rgb(51,51,51); margin-top: 1.6em; margin-bottom: 0.6em; font-size: 20px; white-space: normal;"><span style="Box-sizing: border-Box; font-weight: 700;">apicloud 图片上传,ajax图片上传
Meta charset="utf-8"> <Meta name="viewport" content="maximum-scale=1.0,minimum-scale=1.0,user-scalable=0,width=device-width,initial-scale=1.0"/> <Meta name="format-detection" content="telephone=no,email=no,date=no,address=no">上传function save(data)
{
api.ajax({
url: 'http://192.168.0.163:81/api-v1/driver/task/receipt', method: 'post', report:true,//回调上传进度
data: {files:{file:data},values:{id:39}}
}, err) {
if (ret) {
//成功 可加载回调进度
console.log(JSON.stringify(ret));
} else {
console.log(JSON.stringify(err));
}
});
}
</script>
</body>
</html>
laravel框架:
* 上传图片 */ public function orderReceiptUpload($request) { $obj = new \stdClass(); if( $request->file('file') == false ) { $obj->status = 1; $obj->msg = '上传失败'; return $obj; } //检验文件类型 $fileTypes = array('image/jpeg','image/png','image/jpg'); if(!in_array($request->file('file')->getMimeType(),$fileTypes)) { $obj->status = 1; $obj->msg = '文件格式不正确'; return $obj; } //检验大小 $fileSize= $request->file('file')->getSize(); if(!$request->file('file')->getSize() || $fileSize>2097152 ) { $obj->status = 1; $obj->msg = $fileSize."图片大小不能低于0或超过2048kb"; return $obj; } try {$file = $request->file('file');
// 检验一下上传的文件是否有效.
if($file->isValid())
{
//上传文件的后缀.
$fix = $file->getClientOriginalExtension();
$newName = md5(date("Y-m-d H:i:s")).".".$fix;
$dir = '/uploads/'.date("Y-m-d").'/';
$file->move(public_path().$dir,$newName);
}
$obj->status = 0;
$obj->msg = '上传成功';
return $obj;
} catch (\Exception $e)
{
$obj->status = 1;
$obj->msg = '上传失败';
return $obj;
}
}