5

我在上传过程中使用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');
    }
}
}
4

3 回答 3

9

To only renders the headers of the response, not the body, set

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

Same with redirect()

Docs: http://li3.me/docs/lithium/action/Controller::render

于 2012-02-04T10:33:30.570 回答
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 标头来使用“内容类型协商” :

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

您可以在此处阅读有关如何配置控制器呈现方式的更多信息。

于 2012-02-03T21:35:05.980 回答
0

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

$this->_render['head'] = true;
于 2014-08-14T09:47:45.000 回答