3
  • 我有一个关于 jquery 文件上传的示例,并创建了一个运行良好的演示应用程序,我能够很好地上传文件。
  • 但是我在另一个应用程序中进行了相同的集成,但它无法正常工作,这里的区别只是演示具有 .erb 格式,而这个应用程序具有 .haml 格式。
  • 在这里,我也可以上传高达 100mb 的文件,但超过 100mb 的文件需要时间,并且在服务器日志中显示“无内存错误 - 分配内存失败”。它再次开始上传。
  • 当我取消后刷新页面时,我看到文件被多次上传。我无法找到导致此问题的正确原因,是否是内存不足的问题,如果是,演示应用程序也可以正常工作,我通过它上传了高达 6gb。
  • 这是我的代码

    应用程序.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 我试图在这里添加我的索引文件,但编辑器不接受它。这样你就可以参考这个链接

为此,我使用了 rails 4、ruby 2.1.0 和文件上传使用回形针和 jquery-fileupload-rails gem 在此先感谢。

4

1 回答 1

1
  • 我修复了这个问题,我比较了我的演示应用程序和这个应用程序的 gem 文件,发现这里的 rails 版本是 4.0.2,而在演示应用程序中它是 4.1.0。所以更新了这个宝石,瞧,一切都很好。
  • 在更新到 4.1.0 之后还有一件事我有一个错误,因为“nil:NilClass 的未定义方法‘环境’”这是由于 sass-rails。我也必须更新它,并且我还需要将 sprockets 设置为 1.11.0 版本,因为在捆绑安装后它会更新到 1.12.0。
  • 希望这可以帮助某人
于 2014-04-10T08:14:25.563 回答