ruby 通过Rails中的ajax / javascript文件下载

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 通过Rails中的ajax / javascript文件下载相关的知识,希望对你有一定的参考价值。

# source https://stackoverflow.com/questions/16242359/how-to-trigger-download-with-rails-send-data-from-ajax-post#17526267
create a function in controller

def ajax_download
  send_file "path_to_file/" + params[:file]
end
and then in controller action

respond_to do |format|
  @java_url = "/home/ajax_download?file=#{file_name}"
  format.js {render :partial => "downloadFile"}
end
and make a partial in view folder name with _downloadFile.js.erb and write this line

window.location.href = "<%=@java_url %>"

以上是关于ruby 通过Rails中的ajax / javascript文件下载的主要内容,如果未能解决你的问题,请参考以下文章