如何从Rails路由下载xls文件?

时间:2017-12-14 12:14:42

标签: ruby-on-rails angularjs ruby-on-rails-3 xls

在我的AngularJS应用程序中,我正在调用一个路由(来自Rails应用程序),它返回一个xls文件的内容(如下所示):

<?xml version="1.0"?>
<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:o="urn:schemas-microsoft-com:office:office"
  xmlns:x="urn:schemas-microsoft-com:office:excel"
  xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet"
  xmlns:html="http://www.w3.org/TR/REC-html40">
  <Worksheet ss:Name="Sheet1">
    <Table>
      <Row>
        <Cell><Data ss:Type="String">Ecpm</Data></Cell>
        <Cell><Data ss:Type="String">Total Revenue</Data></Cell>
      </Row>
      .
      .
      .
      <Row>
        <Cell><Data ss:Type="String">4.9</Data></Cell>
        <Cell><Data ss:Type="String">1587.25</Data></Cell>
      </Row>
    </Table>
  </Worksheet>
</Workbook>

控制器的方法就像

def medias_served_export
  @medias_served = query_medias_served_export_by_context('Site')
  respond_to do |format| 
    format.xls { send_data @medias_served, filename: 'teste.xls', disposition: 'attachment', type: 'application/vnd.ms-excel' }
  end
end

网址类似于 /api/statistics/earnings/medias_served_export/20171001/20171030.xls

如何下​​载文件而不是只接收所有文字?后端是否应该在路线的方法中设置额外的标题?

0 个答案:

没有答案