Question

  • I have a referred the example for jquery file upload and created a demo app which works fine and i am able to upload files well.
  • but the same integration i did in another application and it is not working properly the difference here is just that demo had a .erb format and this app have .haml format.
  • here also i am able to upload files upto 100mb but files above that are taking time and in the server log it is displaying "No Memory Error - failed to allocate memory". and it again starts uploading.
  • and when i refresh the page after cancelling i see the file being uploaded multiple times. i am unable to find the proper cause for this problem, is it an issue of low RAM and if it is, the same is working fine for demo app, i have uploaded upto 6gb through that.
  • here is my code

    application.js

    $(document).ready(function(){
    var fileUploadErrors = {
      maxFileSize: 'File is too big',
      minFileSize: 'File is too small',
      acceptFileTypes: 'Filetype not allowed',
      maxNumberOfFiles: 'Max number of files exceeded',
      uploadedBytes: 'Uploaded bytes exceed file size',
      emptyResult: 'Empty file upload result'
      };
    $('#fileupload').fileupload({
      autoUpload : true,        
      maxRetries : 100,
      retryTimeout : 500,
      fail : function(e, data) {
        var fu = $(this).data('blueimp-fileupload') || $(this).data('fileupload'), retries = data.context.data('retries') || 0, retry = function() {
          $.getJSON('#fileupload', {
            file : data.files[0].name
          }).done(function(result) {
            var file = result.file;
            data.uploadedBytes = file && file.size;
            data.data = null;
            data.submit();
          }).fail(function() {
            fu._trigger('fail', e, data);
          });
        };
        if (data.errorThrown !== 'abort' && data.uploadedBytes < data.files[0].size && retries < fu.options.maxRetries) {
          retries += 1;
          data.context.data('retries', retries);
          window.setTimeout(retry, retries * fu.options.retryTimeout);
          return;
        }
        data.context.removeData('retries');
        $.blueimp.fileupload.prototype.options.fail.call(this, e, data);
      }
    });
    $.getJSON($('#fileupload').prop('action'), function (files) {
      var fu = $('#fileupload').data('blueimpFileupload'), 
        template;
      fu._adjustMaxNumberOfFiles(-files.length);
      console.log(files);
      template = fu._renderDownload(files)
        .appendTo($('#fileupload .files'));
      fu._reflow = fu._transition && template.length &&
        template[0].offsetWidth;
      template.addClass('in');
      $('#loading').remove();
    });
    

controller

def index
    @assets = Asset.all
    respond_to do |format|
      format.html
      format.json { render json: @assets.map{|asset| asset.to_jq_asset } }
    end
  end

model

class Asset < ActiveRecord::Base
  has_attached_file :upload
  do_not_validate_attachment_file_type :upload
  include Rails.application.routes.url_helpers
  def to_jq_asset
    {
      "id" => read_attribute(:id),
      "name" => read_attribute(:upload_file_name),
      "size" => read_attribute(:upload_file_size),
      "content_type" => read_attribute(:upload_content_type),
      "url" => upload.url(:original),
      "delete_url" => asset_path(self),
      "delete_type" => "DELETE" 
    }
  end
end
  • index.html.haml I tried to add my index file here but editor is not accepting it. so that you can refer this link

For this I have used rails 4, ruby 2.1.0 and for file upload used paperclip and jquery-fileupload-rails gem Thanks in advance.

Was it helpful?

Solution

  • I fixed this issue, I compared gem file for both my demo app and this app, and found that the version of rails here is 4.0.2 and in demo app it is 4.1.0. So updated this gem and voila everything works great.
  • also one thing after after updating to 4.1.0 I had an error as "undefined method `environment' for nil:NilClass" this was due to sass-rails. I have to update that as well and with that I also needed set sprockets to version 1.11.0 cause after bundle install it gets updated to 1.12.0.
  • hope this helps someone
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top