I am using jquery-file download with Rails 4, I started with https://github.com/tors/jquery-fileupload-rails-paperclip-example . So I use jquery-rails, jquery-fileupload-rails and clip gems.
As I do not crack on jquery or javascript, I try to simplify and understand everything by changing the code to make remote calls on rails with js.erb.
So the file list is a partial rail (_videos.html_erb), and the index action in the uploads controller has index.js.erb for js response. And I added $.get('/uploads'); in the fileupload done event to update partial.
everything works well, if the cancel button , and I do not understand what I need to do and where.
Here is what docs tell me:
How to cancel the download
Download can be canceled by calling the jqXHR object interrupt method:
var jqXHR = $('#fileupload').fileupload('send', {files: filesList}) .error(function (jqXHR, textStatus, errorThrown) { if (errorThrown === 'abort') { alert('File Upload has been canceled'); } }); $('button.cancel').click(function (e) { jqXHR.abort(); });
And this is my index.html.erb:
Here, for the progress bar and text directions, I put the extracted code of the form file-upload-basic-plugin
<div class="container"> <h2 id="titol">Upload file</h2> <%= form_for Upload.new, :html => { :multipart => true, :id => "fileupload" } do |f| %> <div class="row fileupload-buttonbar"> <%= f.file_field :upload %> <button class="cancel">Cancel upload</button> </div> <hr/> <% end %> <div id="videos"> <%= render partial: "videos" %> </div> <script id="template-upload" type="text/x-tmpl"> </script> <script id="template-download" type="text/x-tmpl"> </script> <script type="text/javascript" charset="utf-8"> $('#fileupload').fileupload({ dataType: 'json', add: function (e, data) { data.context = $('<p/>').text('Uploading...').appendTo(document.body); data.submit(); }, done: function (e, data) { data.context.text('Upload finished.'); $.get('/uploads'); } }); $('#fileupload').fileupload({ progressall: function (e, data) { $("#titol").text(data.loaded); var progress = parseInt(data.loaded / data.total * 100, 10); $('#progress .bar').css( 'width', progress + '%'); } }); </script>
I suppose I need to put something like this "var xhr ="
$('#fileupload').fileupload({ dataType: 'json', add: function (e, data) { data.context = $('<p/>').text('Uploading...').appendTo(document.body); var xhr = data.submit(); }, done: function (e, data) { data.context.text('Upload finished.'); $.get('/uploads'); } });
and then
$(function () { $('button.cancel').click(function (e) { jqXHR.abort(); }); })
but this will not work, and the code from docs crashes everywhere: filesList doesn't exist ... etc.
Well, I think I need some basic jquery or javascript tutorial
Thanks in advance