JS

首页 -  JS  -  apicloud 图片上传

apicloud 图片上传

apicloud 图片上传,ajax图片上传

页面触发标签

<!doctype html>
<html>
<head>
    <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">
    <title>slidPane</title>
    <link rel="stylesheet" type="text/css" href="../css/api.css"/>
</head>
<body>
<span onclick="addImg()">上传</span>
<script type="text/javascript" src="../script/api.js"></script>
<script>
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));
      }
  });
}
</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;
    }
}


(1)
分享:

本文由:xiaoshu168 作者:xiaoshu发表,转载请注明来源!

标签:

相关阅读