如何通过AJAX-Call启动下载或使用SlimPHP在服务器上保护文件?

我正在使用SlimPHP作为Backend开发AngularJS WebApp。</ p>

我实现了 基本令牌认证(自定义HTTP - 标题“x-myapp-auth”)。 我的“用户”中间件通过AJAX调用对应用程序中的每个状态更改验证令牌。
需要将一些JSON - 数据转换为XLS,我在服务器端执行SlimPHP。</ p >

现在回答我的问题:
我能怎样</ p>


  1. 使用Slim中的Auth-Token在服务器上保护我的XLS文件,以便 我可以在我的应用程序中有一个默认的下载href </ li>
  2. 当我打开“XLS Stream”时,让浏览器识别出我要发起下载(对不起,我不知道怎么做 从我在SlimPHP中的路线更好地描述。</ li>
    </ ol>

    (2)的输出代码为:</ p>

      header('Content-Type:application / vnd.ms-excel'); 
    header('Content-Disposition:attachment; filename =“'。$ jsonData-&gt; title。'。xls”');
    header( 'Cache-Control:max-age = 0');
    $ objWriter = PHPExcel_IOFactory :: createWriter($ objPHPExcel,'Excel5');
    $ objWriter-&gt; save('php:// output'); \ n </ code> </ pre>

    但是当我通过AJAX检索输出时,我无法使浏览器初始化 吃了文件下载。</ p>
    </ div>

展开原文

原文

I am developing an AngularJS WebApp with SlimPHP as Backend.

I implemented a basic Token-Authentication (custom HTTP - header "x-myapp-auth"). The token is validated by my "User" middleware on every state change in the app via an AJAX-Call. There is the need for converting some JSON - Data to XLS, which I do on the server side within SlimPHP.

Now to my question: How can I

  1. either secure my XLS files on the Server with my Auth-Token in Slim so that I can have a default download href to it in my app
  2. make the Browser recognize I want to initiate a download, when I open a "XLS Stream" (sorry, I don't know how to describe it better) from my route in SlimPHP.

The output code for (2) would be:

header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$jsonData->title.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');

But when I retrieve that output via AJAX, I can't make the browser initiate a file download.

dongxin9759
dongxin9759 如果您使用会话或cookie处理它会怎么样?
大约 5 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问