【问题标题】:Download report from jsreport and angularjs从 jsreport 和 angularjs 下载报告
【发布时间】:2016-01-28 20:21:19
【问题描述】:

我想知道如何使报告可以从我的 AngularJS 应用程序中下载?

报告是.xlsx我可以发布数据,但是回复是:

我想要的是一个可下载的文件,或者在预览中的其他选项卡中打开 Excel Online 上的.xlsx

我怎么能这样做?

【问题讨论】:

    标签: angularjs excel jsreport


    【解决方案1】:

    我通常建议创建一个隐藏的表单并将普通的 http 提交到 jsreport /api/report。这是最稳定的方式,适用于所有浏览器。

    <form method='POST' target='_blank' action='/api/report' id='jsrForm'>
        <input hidden='true' name='template[shortid]' value="41ucBgXKe"/>
        <input hidden='true' name='data[foo]' value="Hello world"/>
        <input hidden='true' name='options[Content-Disposition]' value="attachment; filename=myreport.pdf"/>
    </form>
    
    
    
    <script>
        document.getElementById("jsrForm").submit();
    </script>
    

    【讨论】:

    • 这会在其他选项卡上打开报告吗?如何在 Excel Online 上直接打开.xlsx
    • 这将打开一个下载对话框。要在线打开 excel,您可以删除 Content-Disposition 并添加值为 true 的 options[preview]。但请注意,这将通过公共 jsreport 服务器在线代理 excel。 github.com/jsreport/jsreport/issues/130
    【解决方案2】:

    您可以控制响应吗?如果是这样,请将 content-disposition 标头和 MediaType 标头添加到响应中:

    对于 System.Net.Http.HttpResponseMessage

    var response = new HttpResponseMessage{Content = ...........}
    response.Content.Headers.ContentDisposition = new ContentDispositionHeaderValue("attachment") {
                        FileName = "mydoc.xlsx"
                    };    
    response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    

    对于 System.Net.WebClient

    var client = new WebClient();
    client.Headers.Add("Content-disposition", "attachment");
    client.Headers.Add("Content-Type", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-02-03
      • 1970-01-01
      • 1970-01-01
      • 2018-09-29
      • 1970-01-01
      • 2017-08-24
      • 1970-01-01
      相关资源
      最近更新 更多