-1

我创建了一个 getAction,它根据参数(文件名、日期)下载 zip 存档,该操作运行良好,当我 ping URL 时,下载了 zip。

注意:当我添加 ob_start 服务器给我一个 500 错误

获取操作:

public function getAction()
{
    $fileName = $this->_getParam('fileName');
    $mounth = $this->_getParam('mounth');
    $year = $this->_getParam('year');
    $typDepot = $this->_getParam('typDepot');
    
    $localDir = APPLICATION_DATA.'\viapost\\'.$typDepot.'\Archives\Declaration\\'.$year.'\\'.$mounth;
    $localDir = str_replace("\\", "/", $localDir);
    

    if (file_exists($localDir.'/'.$fileName)) {
    $this->getResponse()
            ->setHeader('Content-Type', 'application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip, application/download, application/force-download')
            ->setHeader('Content-Disposition', 'attachment; filename="'.basename($fileName).'"')
            ->setHeader('Content-Transfer-Encoding', 'binary')
            ->setHeader('Expires', 'Mon, 26 Jul 1997 05:00:00 GMT')
            ->setHeader('Cache-Control', 'no-cache, must-revalidate')
            ->setHeader('Pragma', 'private  ')
            ->setHeader('Content-Description', 'File Transfer')
            ->setHeader('Content-Length', filesize($localDir.'/'.$fileName));

    ob_clean();   
    ob_end_flush();
    
    readfile($localDir.'/'.$fileName);

    $this->getResponse()->sendResponse();
    exit();
    }
    else{
        throw new Zend_Controller_Action_Exception('File not found', 404);
    }
}

另一台服务器必须 ping url 才能从该服务器下载 zip 文件,问题是 zip 文件没有下载,但正在显示带有奇怪字符的字符串。

调用 getAction 的函数:

public function totofunction(){
    $this->_helper->layout->disableLayout();
    $this->_helper->viewRenderer->setNoRender();
    // Récupération du paramètre de recherche.
    //$aRequestParams = $this->_getAllParams();

    $errors = array();  
    //$hostnameViapost = Zend_Registry::get('domainPecWs');
    
    try {
        $client = new Zend_Rest_Client('http://ebordereaurec.viapost-services.intra');
    
        // Set headers
        $client->setHeaders(array(
        'Accept-encoding' => 'gzip,deflate',
        'X-Powered-By' => 'Zend Framework',
        'Content-Type' => 'application/zip;',
        'Accept' => 'application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip',
        'Connection' => 'Keep-Alive'
        ));
        // Adding several parameters with one call
        $get_datas = array(
            'fileName'   => '2019-02-18-202.zip',//$aRequestParams['nomFichier'],
            'typDepot'   => 'DRP',//$aRequestParams['typDepot'],
            'year'       => '2020',//$aRequestParams['year'],
            'mounth'     => '06'//$aRequestParams['mounth']
        );
    
        $result = ($client->restGet('/viapost/DownloadCreation/',$get_datas)); 
        
    } catch(Zend_Rest_Client_Exception $e) {
        // catch Rest Client Exception
        $errors[] = '[' . $e->getCode() . ']:' . $e->getMessage();
    }
    catch (Exception $e) {
        // catch general exception if any occurs
        // like DB
        $errors[] = '[' . $e->getCode() . ']:' . $e->getMessage();
    }

    if ($result->isError()) {
      echo "Les infos Serveur sont : "
         . $result->getStatus()
         . " " . $result->getMessage()
         . "\n";
    }
    echo '<pre>';
    return $result;
    echo '</pre>';
}

我得到的东西: 结果和显示的读取文件

我已经尝试了我在互联网上找到的关于使用 getAction 和 Zend REST 下载文件夹的所有内容,但它仍然显示奇怪的字符串。因此,如果有人有一个很棒的想法。

4

1 回答 1

0
setHeader('Content-Type', 'application/zip, application/octet-stream, application/x-zip-compressed, multipart/x-zip, application/download, application/force-download')

goAction 中的这一行看起来很奇怪,我认为它导致浏览器无法确定确切的内容类型。然后浏览器将内容视为 text/html。

尝试这个:

setHeader('Content-Type', 'application/zip')
于 2020-06-25T08:52:55.437 回答