dpfl37651 2015-11-17 19:17
浏览 77
已采纳

无法使用Google Chrome从laravel响应中下载简单的CSV流

When I go the /test-csv route in Safari/Firefox, I am able to download the one-line csv file. But when I try it in Chrome, it says 'Failed to load Response Data' in the Network tab of the console.

*edit*

IMPORTANT NEW DETAIL: It does work in Chrome on a Windows Computer, but not on my Macbook Pro

*edit*

I have tried several variations of the Content-Type and Content-Disposition headers to no avail.

When I leave out all headers, I get just a string in the browser: "id,name,email" (no download).

Do I need some kind of special header for Chrome to let me me force a download of this streamed data?

use Symfony\Component\HttpFoundation\StreamedResponse;

Route::get('test-csv', function(){

        $response = new StreamedResponse(function(){
            // Open output stream
            $handle = fopen('php://output', 'w');
            fputcsv($handle, [
                'id',
                'name',
                'email'
            ]);
            fclose($handle);

        }, 200, [
            'Content-Type' => 'text/csv',
            'Content-Disposition' => 'attachment;filename=export.csv;',
        ]);


        return $response;
    });
  • 写回答

1条回答 默认 最新

  • dshyu6866 2015-11-18 13:07
    关注

    @Bogdan's comment made me realize that the problem must have been in my install/settings in Chrome.

    I turned off extensions and uninstalled and reinstalled chrome. It is working as expected now.

    If @bogdan wants to post his comment/suggestion as an answer, I will mark it as the accepted answer.

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

报告相同问题?

悬赏问题

  • ¥15 孟德尔随机化结果不一致
  • ¥20 求用stm32f103c6t6在lcd1206上显示Door is open和password:
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法