【问题标题】:How to Download xlsx file using gem "axlsx" and "axlsx_rails" from rails 4 application using AJAX call?如何使用 AJAX 调用从 rails 4 应用程序中使用 gem“axlsx”和“axlsx_rails”下载 xlsx 文件?
【发布时间】:2020-10-04 11:51:51
【问题描述】:

我正在尝试在 Rails 4 应用程序中使用 axlsxaxlsx_rails 下载 xlsx 文件。

我的报告控制器:

def generate_report
@reports = Report.all
     respond_to do |format|
    format.xlsx {
      response.headers[
        'Content-Disposition'
      ] = "attachment; filename='items.xlsx'"
    }
end

app/views/reports/ 中创建generate_report.xlsx.axlsx 文件。

这是点击下载按钮时调用的代码:

$.ajax({
  type: 'GET',
  url: '/reports/generate_report',
  data: {
    user_id: user_id
  },
  success: function(response) {
    return console.log('file downloaded');
  }
});

【问题讨论】:

    标签: ajax ruby-on-rails-4 xlsx axlsx


    【解决方案1】:

    您可以将您的 xlsx 文件转换为 Base64 然后发送。

    查看send_data Rails 方法以发送您的Base64 工作表。

    来源:Rails - axlsx_rails, generating xlsx and sending via API

    【讨论】:

    • 感谢分享。但这个解决方案对我不起作用。
    • 我给了你应该使用的方法。来源是我从中获得解决方案的地方。我没有为您提供完整的解决方案,您必须解决它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-03
    • 2011-09-17
    • 1970-01-01
    • 2012-02-15
    • 2015-11-09
    相关资源
    最近更新 更多