dongping6974 2013-09-27 14:00
浏览 35
已采纳

CakePHP:使用$ this-> response-> file()时如何设置文件名

We're sending a zip file download as a response like this:

    $this->response->file( "/export/stuff.zip", array('downlaod'=>true, 'name'=>"stuff.zip") );
    return $this->response;

This works fine, BUT the file is always named export.zip. Our name option does not seem to have any effect. We've also tried without the .zip extension. This is confusing because the name options is shown here, in the docs.

What are we doing wrong?

Update: We figured out that the seemingly arbitrary name "export" is being copied from the name of the controller action. We changed the method name to "admin_exportt" and then we get exportt.zip every time. This isn't documented anywhere that I've seen.

We found where the name is handled in the source code (/lib/Cake/Nework/CakeResponse.php:1254) and it appears that it should either use the original file name, or whatever is specified in the name options:

        if (is_null($options['name'])) {
            $name = $file->name;
        } else {
            $name = $options['name'];
        }
  • 写回答

1条回答 默认 最新

  • dsk49208 2013-09-27 18:41
    关注

    Ugh! We figured out what was wrong...

    Notice the word downlaod in the first line of my code above? That's the culprit. Apparently that bad option was causing the entire array to get ignored. I'm not sure if this will help anyone in the future, but I guess I'll leave it up as a reminder that CakePHP options work that way (at lease in this context).

    PS: Whenever you're stuck, go take a walk and come back!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!