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 python中合并修改日期相同的CSV文件并按照修改日期的名字命名文件
  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员