jquery.fileDownload.js package for Rails 3.1+ and Rails 4 asset pipeline
Switch branches/tags
Clone or download

README.md

jquery_file_download-rails

Install gem Build status

Packages the jquery.fileDownload.js script for use with the Rails 3.1+ and Rails 4 asset pipeline.

Installation

  • Add jquery_file_download-rails to your application's Gemfile:
gem 'jquery_file_download-rails'
  • Run Bundler to lock the gem:
bundle install
  • Add a reference to the gem's assets to your application's JavaScript asset manifest (usually app/assets/javascripts/application.js) immediately after the require directive for jquery and jquery_ufs:
//= require jquery.fileDownload
  • With the gem installed, the $.fileDownload jQuery plugin is available to JavaScripts, for example:
$(function () {
  $("#download").on("click", function (e) {
    e.preventDefault();
    alert("Download link clicked");
    $.fileDownload($(this).prop("href"), {
      successCallback: function (url) { alert("Success"); },
      failCallback: function (url) { alert("Fail"); }
    });
  });
});
  • Actions sending files in response to download requests must set the fileDownload cookie which can be done via the cookies collection as in this sample controller:
class DownloadController < ApplicationController
  def some_file
    cookies['fileDownload'] = 'true'

    send_file 'path-to-file-on-server',
      filename: 'browser-download-file-name',
      type: 'content-type',
      x_sendfile: true
  end
end

Step-by-step instructions and sample applications

Links

Licence

jquery_file_download-rails is released under the MIT licence.