public function upload($maxFileSize = null, $acceptFileTypes = null) {
		Configure::write('debug', 0);
		if (!$this->_controller->request->is('ajax') || !$this->_controller->RequestHandler->prefers('json')) {
			throw new BadRequestException(__d('view_extension', 'Invalid request'));
		}

		$uploadDir = $this->getUploadDir();
		if (empty($acceptFileTypes)) {
			$acceptFileTypes = '/.+$/i';
		}

		$opt = [
			'script_url' => '',
			'upload_url' => '',
			'param_name' => 'files',
			'upload_dir' => $uploadDir,
			'max_file_size' => $maxFileSize,
			'image_versions' => [
				'' => ['auto_orient' => false]
			],
			'correct_image_extensions' => false,
			'accept_file_types' => $acceptFileTypes
		];
		$uploadHandler = new UploadHandler($opt, false);

		switch ($this->_controller->request->method()) {
			case 'OPTIONS':
			case 'HEAD':
				$data = $uploadHandler->head(false);
				break;
			case 'GET':
				$data = $uploadHandler->get(false);
				break;
			case 'PATCH':
			case 'PUT':
			case 'POST':
				$data = $uploadHandler->post(false);
				break;
			default:
				throw new MethodNotAllowedException();
		}

		return $data;
	}
public function upload($maxFileSize = null, $acceptFileTypes = null) {
		Configure::write('debug', 0);
		if (!$this->_controller->request->is('ajax') || !$this->_controller->RequestHandler->prefers('json')) {
			throw new BadRequestException(__d('view_extension', 'Invalid request'));
		}

		$uploadDir = $this->getUploadDir();
		if (empty($acceptFileTypes)) {
			$acceptFileTypes = '/.+$/i';
		}

		$opt = [
			'script_url' => '',
			'upload_url' => '',
			'param_name' => 'files',
			'upload_dir' => $uploadDir,
			'max_file_size' => $maxFileSize,
			'image_versions' => [
				'' => ['auto_orient' => false]
			],
			'correct_image_extensions' => false,
			'accept_file_types' => $acceptFileTypes
		];
		$uploadHandler = new UploadHandler($opt, false);

		switch ($this->_controller->request->method()) {
			case 'OPTIONS':
			case 'HEAD':
				$data = $uploadHandler->head(false);
				break;
			case 'GET':
				$data = $uploadHandler->get(false);
				break;
			case 'PATCH':
			case 'PUT':
			case 'POST':
				$data = $uploadHandler->post(false);
				break;
			default:
				throw new MethodNotAllowedException();
		}

		return $data;
	}
public function upload ( $ maxFileSize = null , $ acceptFileTypes = null ) { Configure :: write ( 'debug' , 0 ) ; if ( ! $ this -> _controller -> request -> is ( 'ajax' ) || ! $ this -> _controller -> RequestHandler -> prefers ( 'json' ) ) { throw new BadRequestException ( __d ( 'view_extension' , 'Invalid request' ) ) ; } $ uploadDir = $ this -> getUploadDir ( ) ; if ( empty ( $ acceptFileTypes ) ) { $ acceptFileTypes = '/.+$/i' ; } $ opt = [ 'script_url' => '' , 'upload_url' => '' , 'param_name' => 'files' , 'upload_dir' => $ uploadDir , 'max_file_size' => $ maxFileSize , 'image_versions' => [ '' => [ 'auto_orient' => false ] ] , 'correct_image_extensions' => false , 'accept_file_types' => $ acceptFileTypes ] ; $ uploadHandler = new UploadHandler ( $ opt , false ) ; switch ( $ this -> _controller -> request -> method ( ) ) { case 'OPTIONS' : case 'HEAD' : $ data = $ uploadHandler -> head ( false ) ; break ; case 'GET' : $ data = $ uploadHandler -> get ( false ) ; break ; case 'PATCH' : case 'PUT' : case 'POST' : $ data = $ uploadHandler -> post ( false ) ; break ; default : throw new MethodNotAllowedException ( ) ; } return $ data ; }
AJAX upload file to server

@param int $maxFileSize Maximum size of uploaded file
@param string $acceptFileTypes The regular expression pattern
to validate uploaded files.
Default `/.+$/i`.
@throws BadRequestException If request is not `AJAX` or not `JSON`
@throws MethodNotAllowedException If request is not in list:
- `OPTIONS`;
- `HEAD`;
- `GET`;
- `PATCH`;
- `PUT`;
- `POST`.
@return array Result of upload file
@link https://github.com/blueimp/jQuery-File-Upload
AJAX upload file to server