Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/265.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 我如何使用typo3/extbase触发下载?_Php_Typo3_Extbase_Fluid - Fatal编程技术网

Php 我如何使用typo3/extbase触发下载?

Php 我如何使用typo3/extbase触发下载?,php,typo3,extbase,fluid,Php,Typo3,Extbase,Fluid,我将Typo3与extbase和fluid一起使用。我有一个控制器,其动作名为downloadAction()。调用操作后,系统尝试呈现下载模板(但我只想开始下载) 如何转储创建的下载文件,并发送下载头而不是正常的渲染过程?最好的方法是什么 谢谢 如何转储创建的下载文件并发送下载头而不是正常的渲染过程 只要是这样,就不会有任何东西阻止您发送标题、发送内容并实际调用exit,尽管您可能希望在这样做之前这样做。您也可以这样做。您可以为下载请求定义特殊的页面类型: download = PAGE do

我将Typo3与extbase和fluid一起使用。我有一个控制器,其动作名为
downloadAction()
。调用操作后,系统尝试呈现下载模板(但我只想开始下载)

如何转储创建的
下载文件
,并发送下载头而不是正常的渲染过程?最好的方法是什么

谢谢

如何转储创建的下载文件并发送下载头而不是正常的渲染过程


只要是这样,就不会有任何东西阻止您发送标题、发送内容并实际调用
exit
,尽管您可能希望在这样做之前这样做。您也可以这样做。

您可以为下载请求定义特殊的页面类型:

download = PAGE
download  {
  typeNum = 1249058993
  10 < tt_content.list.20.efempty_pi1

  config {
   disableAllHeaderCode = 1
   xhtml_cleaning = 0
   admPanel = 0
   additionalHeaders = Content-type:application/octet-stream
    }
  }
}
download=PAGE
下载{
typeNum=1249058993
10
“efempty”必须替换为扩展名

您现在可以按如下方式触发下载:

<f:link.action pageType="1249058993" action="download" controller="ControllerName">Download</f:link.action>
下载

几个月前,我在一个项目中做了这件事,非常简单

    /**
     * @param string $fileName
     * @return void
     */  
    public function downloadAction($fileName) {

        $file = $this->settings['uploadFolder'] . 'uploadedPhotos/' . $fileName;        

        if(is_file($file)) {

            $fileLen    = filesize($file);          
            $ext        = strtolower(substr(strrchr($fileName, '.'), 1));

            switch($ext) {
                case 'txt':
                    $cType = 'text/plain'; 
                break;              
                case 'pdf':
                    $cType = 'application/pdf'; 
                break;
                case 'zip':
                    $cType = 'application/zip';
                break;
                case 'doc':
                    $cType = 'application/msword';
                break;
                case 'xls':
                    $cType = 'application/vnd.ms-excel';
                break;
                case 'ppt':
                    $cType = 'application/vnd.ms-powerpoint';
                break;
                case 'gif':
                    $cType = 'image/gif';
                break;
                case 'png':
                    $cType = 'image/png';
                break;
                case 'jpeg':
                case 'jpg':
                    $cType = 'image/jpg';
                break;
                case 'mp3':
                    $cType = 'audio/mpeg';
                break;
                case 'wav':
                    $cType = 'audio/x-wav';
                break;
                case 'mpeg':
                case 'mpg':
                case 'mpe':
                    $cType = 'video/mpeg';
                break;
                case 'mov':
                    $cType = 'video/quicktime';
                break;
                case 'avi':
                    $cType = 'video/x-msvideo';
                break;

                //forbidden filetypes
                case 'inc':
                case 'conf':
                case 'sql':                 
                case 'cgi':
                case 'htaccess':
                case 'php':
                case 'php3':
                case 'php4':                        
                case 'php5':
                exit;

                case 'exe':                 
                default:
                    $cType = 'application/octet-stream';
                break;
            }

            $headers = array(
                'Pragma'                    => 'public', 
                'Expires'                   => 0, 
                'Cache-Control'             => 'must-revalidate, post-check=0, pre-check=0',                    
                'Content-Description'       => 'File Transfer',
                'Content-Type'              => $cType,
                'Content-Disposition'       => 'attachment; filename="'. $fileName .'"',
                'Content-Transfer-Encoding' => 'binary', 
                'Content-Length'            => $fileLen         
            );

            foreach($headers as $header => $data)
                $this->response->setHeader($header, $data); 

            $this->response->sendHeaders();                 
            @readfile($file);   

        }   
        exit;   
    }
更新: 这实际上不适用于激活的压缩,因为内容长度仍然表示未压缩的大小。eID可以用作解决方案事实上甚至有官方呼吁:

eID=“dumpFile”参见typo3/sysext/core/Resource/PHP/FileDumpEID.PHP

然而,这个调用不会强制下载,而是“转储”它。我已经制作了一张@Forge的票证来解决这个问题(该票证已被接受,并针对LTS 7):

旧答案: 目前实现这一目标最简单的方法是:

打字稿常量 打字稿设置 流质
下载
与其他提到的答案相比,好处如下:

  • 正确的标题(包括mime/类型)
  • Extbase格式而不是typenum
旁注:iPhone忽略内容处置标题(始终为内联)


关于“退出”问题,我还没有对其进行测试,但对于页面类型,如果您使用自己的PHPView(StandaloneView?),它可能会起作用。

一定有更好的方法。我希望避免使用exit。唯一的其他选项是
return
ing,此时控制将返回到框架,并将根据需要继续处理请求,即启动将有效干扰下载的无用助手。一次粗鲁的退出可能不会有什么害处。ASAIK,没有
应用程序/强制下载
,它是一种无法识别的MIME类型,并返回到
应用程序/八位字节流
,这将是正确使用的MIME类型。
    /**
     * @param string $fileName
     * @return void
     */  
    public function downloadAction($fileName) {

        $file = $this->settings['uploadFolder'] . 'uploadedPhotos/' . $fileName;        

        if(is_file($file)) {

            $fileLen    = filesize($file);          
            $ext        = strtolower(substr(strrchr($fileName, '.'), 1));

            switch($ext) {
                case 'txt':
                    $cType = 'text/plain'; 
                break;              
                case 'pdf':
                    $cType = 'application/pdf'; 
                break;
                case 'zip':
                    $cType = 'application/zip';
                break;
                case 'doc':
                    $cType = 'application/msword';
                break;
                case 'xls':
                    $cType = 'application/vnd.ms-excel';
                break;
                case 'ppt':
                    $cType = 'application/vnd.ms-powerpoint';
                break;
                case 'gif':
                    $cType = 'image/gif';
                break;
                case 'png':
                    $cType = 'image/png';
                break;
                case 'jpeg':
                case 'jpg':
                    $cType = 'image/jpg';
                break;
                case 'mp3':
                    $cType = 'audio/mpeg';
                break;
                case 'wav':
                    $cType = 'audio/x-wav';
                break;
                case 'mpeg':
                case 'mpg':
                case 'mpe':
                    $cType = 'video/mpeg';
                break;
                case 'mov':
                    $cType = 'video/quicktime';
                break;
                case 'avi':
                    $cType = 'video/x-msvideo';
                break;

                //forbidden filetypes
                case 'inc':
                case 'conf':
                case 'sql':                 
                case 'cgi':
                case 'htaccess':
                case 'php':
                case 'php3':
                case 'php4':                        
                case 'php5':
                exit;

                case 'exe':                 
                default:
                    $cType = 'application/octet-stream';
                break;
            }

            $headers = array(
                'Pragma'                    => 'public', 
                'Expires'                   => 0, 
                'Cache-Control'             => 'must-revalidate, post-check=0, pre-check=0',                    
                'Content-Description'       => 'File Transfer',
                'Content-Type'              => $cType,
                'Content-Disposition'       => 'attachment; filename="'. $fileName .'"',
                'Content-Transfer-Encoding' => 'binary', 
                'Content-Length'            => $fileLen         
            );

            foreach($headers as $header => $data)
                $this->response->setHeader($header, $data); 

            $this->response->sendHeaders();                 
            @readfile($file);   

        }   
        exit;   
    }
# ASCII "download" in Numbers (4-15-23-14-12-15-1-4) - See http://rumkin.com/tools/cipher/numbers.php
plugin.tx_extensionname.view.formatToPageTypeMapping.download = 4152314121514
tx_extensionname_download = PAGE
tx_extensionname_download {
    typeNum < plugin.tx_extensionname.view.formatToPageTypeMapping.download
    config {
        disableAllHeaderCode = 1
        xhtml_cleaning = 0
        admPanel = 0
        debug = 0
        no_cache = 1
    }

    10 = USER
    10 {
        userFunc = TYPO3\CMS\Extbase\Core\Bootstrap->run
        extensionName = ExtensionName
        pluginName = PluginName
        vendorName = VENDOR
        controller = ControllerName
        action = download

        view < plugin.tx_extensionname.view
        persistence < plugin.tx_extensionname.persistence
        settings < plugin.tx_extensionname.settings
    }
}
/**
 * Download
 *
 * @param \VENDOR\ExtensionName\Domain\Model\Model $model
 * @return void
 * @ignorevalidation $model
 */
public function downloadAction($model) {
    if ($model->getFile()) {
        $model->getFile()->getOriginalResource()->getOriginalFile()->getStorage()->dumpFileContents(
            $model->getFile()->getOriginalResource(),
            $asDownload = TRUE,
            $alternativeFilename = $model->getFile()->getOriginalResource()->getName()
        );
        exit;
    }

    $this->throwStatus(
        $statusCode = 404,
        $statusMessage = 'Not Found'
    );
}
<f:link.action controller="ControllerName" pluginName="PluginName" action="download" arguments="{model: '{model.uid}'}" format="download" title="{model.file.originalResource.title}">Download</f:link.action>