禁用锂中的渲染过程

时间:2012-02-03 18:22:46

标签: php uploadify lithium

我使用uploadify进行上传过程。问题是每次上传后锂都会尝试渲染控制器的视图。在我的情况下uploadify.html.php。如何禁用此行为并返回200 OK。

我的控制器代码:

class UploadController extends \app\controllers\AppController {

public function index() {}

public function uploadify() {
    Logger::write('info', 'start upload');

    if (!empty($this->request->data)) {
        $fileData = $this->request->data['Filedata'];
        $error = $fileData['error'];
        if($error == UPLOAD_ERR_OK) {
            // everything ok
            $tempFile = $fileData['tmp_name'];
            $targetPath = $this->request->env('DOCUMENT_ROOT') . $fileData['folder'] . '/';
            $targetFile =  str_replace('//','/',$targetPath) . $fileData['name'];
            move_uploaded_file($tempFile, $targetFile);
            Logger::write('info', 'upload file successfull to ' . $targetFile);
        } else if($error == UPLOAD_ERR_INI_SIZE || $error == UPLOAD_ERR_FORM_SIZE) {
            // file size to large
            Logger::write('error', 'file to large ' . $fileData['Filename']);
        } else if($error == UPLOAD_ERR_PARTIAL) {
            // only partial uplopad
            Logger::write('error', 'uploaded partial ' . $fileData['Filename']);
        } else if($error == UPLOAD_ERR_NO_FILE) {
            // no file uploaded
            Logger::write('error', 'couldn\'t upload ' . $fileData['Filename']);
        } else {
            Logger::write('error', 'Unknown error code ' . $error);
        }
    } else {
        Logger::write('error', 'no form data');
    }
}
}

3 个答案:

答案 0 :(得分:9)

要仅呈现响应的标题,而不是正文,请设置

$this->render(array('head' => true))

redirect()

相同

文档:http://li3.me/docs/lithium/action/Controller::render

答案 1 :(得分:0)

你可以通过以下两种方式之一来解决这个问题。

第一种方法是关闭自动渲染:

class MyController extends \lithium\action\Controller {
  public function __construct(array $config = array()) {
    $defaults = array('render' => array('auto' => false));
    return parent::__construct($config + $defaults);
  }
}

第二种方法是通过发送HTTP Accepts header

来使用“内容类型协商”
class MyController extends \lithium\action\Controller {
  public function __construct(array $config = array()) {
    $defaults = array('render' => array('negotiate' => true));
    return parent::__construct($config + $defaults);
  }
}

您可以详细了解如何配置控制器呈现here的方式。

答案 2 :(得分:0)

要解决此问题,您只需在控制器操作中添加以下行:

$this->_render['head'] = true;