apicloud 图片上传,ajax图片上传
页面触发标签html>
slidPane上传
function addImg ()
{
api.getPicture({
sourceType: 'album',
encodingType: 'jpg',
mediaValue: 'pic',
destinationType: 'url',
allowEdit: true,
quality: 80,
saveToPhotoAlbum: false
}, function(ret, err) {
if (ret) {
save(ret.data);
} else {
alert(JSON.stringify(err));
}
});
}
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}}
}, function(ret, err) {
if (ret) {
//成功 可加载回调进度
console.log(JSON.stringify(ret));
} else {
console.log(JSON.stringify(err));
}
});
}
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;
}
}