在jquery文件上传中多次上传大文件

时间:2014-04-09 18:26:02

标签: file-upload ruby-on-rails-4 paperclip jquery-file-upload jquery-fileupload-rails

  • 我有一个jquery文件上传的例子,并创建了一个 演示应用程序工作正常,我能够很好地上传文件。
  • 但我在另一个应用程序中执行了相同的集成,但事实并非如此 正常工作,这里的差异只是该演示有一个.erb 格式和这个应用程序有.haml格式。
  • 这里我也可以上传文件高达100mb,但文件高于100mb 花时间在服务器日志中显示"无记忆 错误 - 无法分配内存"。它再次开始上传。
  • 当我取消后刷新页面时,我看到文件正在 多次上传。我无法找到正确的原因 这个问题,是一个低RAM的问题,如果是,同样的问题 对于演示应用程序工作正常,我通过它上传了高达6GB。
  • 这是我的代码

    的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();
    });
    

控制器

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

模型

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 我试图在这里添加我的索引文件,但编辑器不接受它。以便您可以参考此link

为此,我使用了rails 4,ruby 2.1.0和文件上传使用了paperclip和jquery-fileupload-rails gem 提前谢谢。

1 个答案:

答案 0 :(得分:1)

  • 我解决了这个问题,我比较了我的演示应用程序和它的gem文件 app,发现这里的rails版本是4.0.2并在demo中 应用程序是4.1.0。所以更新了这个宝石,瞧一切正常 大。
  • 在更新到4.1.0之后还有一件事我有一个错误 &#34;未定义的方法`环境&#39;为零:NilClass&#34;这是由于 萨斯护栏。我也必须更新它,我也需要 将sprockets设置为版本1.11.0导致捆绑安装后得到 更新至1.12.0。
  • 希望这有助于某人
相关问题