dsjklb0205 2016-12-20 21:31
浏览 61
已采纳

Zend文件传输适配器Http打破了我的AJAX响应,为什么?

I am working in Zend Framework 1 and I have this function in a controller:

public function uploadAction()
{
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    $data = [];

    if ($this->getRequest()->isPost()) {
        $path = /cronjobs/uploads';

        // Clean $path directory OOP way using SPL
        $di = new RecursiveDirectoryIterator($path, FilesystemIterator::SKIP_DOTS);
        $ri = new RecursiveIteratorIterator($di, RecursiveIteratorIterator::CHILD_FIRST);
        foreach ($ri as $file) {
            $file->isDir() ? rmdir($file) : unlink($file);
        }

        $adapter = new Zend_File_Transfer_Adapter_Http();
        $adapter->addValidator('Extension', false, ['extension' => 'csv', 'case' => true]);
        $adapter->addValidator('MimeType', false, ['extension' => 'text/plain']);

        // Check if the uploaded file is valid
        $errors[] = $adapter->isValid() === false ? $adapter->getMessages() : '';

        $file     = (array) $adapter->getFileInfo()['file'];
        $ext      = end(explode('.', $file['name']));
        $new_path = $file['tmp_name'];

        // Check file size
        $checkFileSize = Attachment::checkMaxfileSize($file['size']);
        if (!$checkFileSize['accept']) {
            echo json_encode($checkFileSize['message']);

            return true;
        }

        $data['file'] = array(
            'name'       => $file['name'],
            'size'       => $adapter->getFileSize(),
            'file_path'  => $new_path,
            'file_ext'   => $ext
        );

        $data['var'] = '';
    } else {
        $data['error'] = 'Invalid request.';
    }

    return $this->_helper->json($data);
}

This method is called trough AJAX as follow:

$('#fileupload').show().fileupload({
    url: url,
    type: "POST",
    cache: false,
    dataType: 'json',
    done: function (e, data) {
        console.log(data.result);
    },
    error: function (xhr, textStatus, errorThrown) {
        console.log(xhr + " " + textStatus + " " + errorThrown);
    }
})

For some reason as soon as I call $adapter->isValid() in the controller the AJAX response gets break. I can say the problem is there because if I comment that piece of code everything works fine.

This is the message I am getting currently:

POST http://localhost/admin/upload net::ERR_EMPTY_RESPONSE
massive_charge_types_file_upload.js:147 [object Object] error

After read all of the following topics:

I am out of ideas and I am stuck since can't find what's causing the behavior.

UPDATE:

I believe the problem is on the isValid() method which return a boolean but for some reason this is breaking my response. Any ideas?

Can any help me with this?

  • 写回答

2条回答 默认 最新

  • dsigg21445 2016-12-20 22:15
    关注

    It seems the syntax of your MimeType validator is wrong:

    $adapter->addValidator('MimeType', false, ['extension' => 'text/plain']);
    

    Should probably be:

    $upload->addValidator('MimeType', false, array('text/plain'));
    

    As described:

    https://framework.zend.com/manual/1.12/en/zend.file.transfer.validators.html

    Since your file won't pass the (impossible) validation test - I am guessing this is what then leads to no results?

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'