【问题标题】:Large file getting uploaded multiple times in jquery file upload在jquery文件上传中多次上传大文件
【发布时间】:2014-04-09 18:26:02
【问题描述】:
  • 我参考了 jquery 文件上传的示例并创建了一个 演示应用程序运行良好,我能够很好地上传文件。
  • 但是我在另一个应用程序中进行了相同的集成,但它不是 正常工作这里的区别只是演示有一个 .erb 格式,并且此应用具有 .haml 格式。
  • 在这里我也可以上传最大 100mb 的文件,但文件超过 正在花费时间,并且在服务器日志中显示“无内存 错误 - 无法分配内存”。它再次开始上传。
  • 当我在取消后刷新页面时,我看到文件正在 多次上传。我找不到正确的原因 这个问题,是不是内存不足的问题,如果是,同样是 演示应用程序运行良好,我通过它上传了高达 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,文件上传使用了回形针和 jquery-fileupload-rails gem 提前致谢。

【问题讨论】:

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


    【解决方案1】:
    • 我修复了这个问题,我比较了我的演示应用程序和这个的 gem 文件 app,发现这里rails的版本是4.0.2和demo 应用程序是 4.1.0。所以更新了这个宝石,瞧一切正常 很棒。
    • 更新到 4.1.0 后还有一件事我有一个错误 “nil:NilClass 的未定义方法‘环境’”这是由于 sass-rails。我也必须更新它,我也需要 将 sprockets 设置为 1.11.0 版本,因为捆绑安装后它会得到 更新到 1.12.0。
    • 希望这对某人有所帮助

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2010-10-08
      • 2011-03-26
      • 1970-01-01
      • 1970-01-01
      • 2023-03-26
      • 2014-12-31
      • 2014-05-04
      • 2015-06-05
      相关资源
      最近更新 更多