1

我是 Rails 新手。

我正在尝试将 BlueImp 的 jQuery-File-Upload 插件实现到 Rails 4 应用程序中。所以我有基本的文件上传工作,但我遇到了多个问题。首先,上传页面的用户界面很奇怪。

我只看到“开始上传”和“取消上传”按钮。即使那样,我也无法单击它们,因为不可见的“添加文件”按钮占据了页面的一半,所以我只能通过以下方式上传文件:单击巨大的、不可见的“添加文件”按钮,选择一个文件,按两次 Tab突出显示“开始上传”按钮,然后按 Enter。上传后,我没有转到任何特定页面,而是转到显示文件上传参数的页面。但是,文件确实上传。我会上传图片,但我显然不能。任何帮助将不胜感激。

这是 video_resources 视图的 _form.html.haml 文件。我认为也许从 html 到 haml 的转换可能是一个问题。

= form_for VideoResource.new, :html => {:multipart => true, :id => "fileupdload" } do |f|
  .row.fileupload-buttonbar
    .span7
      %span.btn.btn-success.fileinput-button
        %i.icon-plus.icon-white
        %span Add files...
        = f.file_field :file_path
      %button.btn.btn-primary.start{:type => "submit"}
        %i.icon-upload.icon-white
        %span Start upload
      %button.btn.btn-warning.cancel{:type => "reset"}
        %i.icon-ban-circle.icon-white
        %span Cancel upload
      %button.btn.btn-danger.delete{:type => "button"}
        %i.icon-trash.icon-white
        %span Delete
      %input.toggle{:type => "checkbox"}/
    .span5
      .progress.progress-success.progress-striped.active.fade
        .bar{:style => "width:0%;"}
  .fileupload-loading
  %br/
  %table.table.table-striped
    %tbody.files{"data-target" => "#modal-gallery", "data-toggle" => "modal-gallery"}

:javascript
  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'
  };
/ The template to display files available for upload
%script#template-upload{:type => "text/x-tmpl"}
  {% for (var i=0, file; file=o.files[i]; i++) { %}
  <tr class="template-upload fade">
  <td class="preview"><span class="fade"></span></td>
  <td class="name"><span>{%=file.name%}</span></td>
  <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
  {% if (file.error) { %}
  <td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
  {% } else if (o.files.valid && !i) { %}
  <td>
  <div class="progress progress-success progress-striped active"><div class="bar" style="width:0%;"></div></div>
  </td>
  <td class="start">{% if (!o.options.autoUpload) { %}
  <button class="btn btn-primary">
  <i class="icon-upload icon-white"></i>
  <span>{%=locale.fileupload.start%}</span>
  </button>
  {% } %}</td>
  {% } else { %}
  <td colspan="2"></td>
  {% } %}
  <td class="cancel">{% if (!i) { %}
  <button class="btn btn-warning">
  <i class="icon-ban-circle icon-white"></i>
  <span>{%=locale.fileupload.cancel%}</span>
  </button>
  {% } %}</td>
  </tr>
  {% } %}
/ The template to display files available for download
%script#template-download{:type => "text/x-tmpl"}
  {% for (var i=0, file; file=o.files[i]; i++) { %}
  <tr class="template-download fade">
  {% if (file.error) { %}
  <td></td>
  <td class="name"><span>{%=file.name%}</span></td>
  <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
  <td class="error" colspan="2"><span class="label label-important">{%=locale.fileupload.error%}</span> {%=locale.fileupload.errors[file.error] || file.error%}</td>
  {% } else { %}
  <td class="preview">{% if (file.thumbnail_url) { %}
  <a href="{%=file.url%}" title="{%=file.name%}" rel="gallery" download="{%=file.name%}"><img src="{%=file.thumbnail_url%}"></a>
  {% } %}</td>
  <td class="name">
  <a href="{%=file.url%}" title="{%=file.name%}" rel="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
  </td>
  <td class="size"><span>{%=o.formatFileSize(file.size)%}</span></td>
  <td colspan="2"></td>
  {% } %}
  <td class="delete">
  <button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
  <i class="icon-trash icon-white"></i>
  <span>{%=locale.fileupload.destroy%}</span>
  </button>
  <input type="checkbox" name="delete" value="1">
  </td>
  </tr>
  {% } %}
/ The jQuery UI widget factory, can be omitted if jQuery UI is already included
= javascript_include_tag 'jquery.ui.widget.js'
/ The Templates and Load Image plugins are included for the FileUpload user interface
%script{:src => "http://blueimp.github.com/JavaScript-Templates/tmpl.min.js"}
%script{:src => "http://blueimp.github.com/JavaScript-Load-Image/load-image.min.js"}
/ The Iframe Transport is required for browsers without support for XHR file uploads
= javascript_include_tag 'jquery.iframe-transport.js'
= javascript_include_tag 'jquery.fileupload.js'
= javascript_include_tag 'jquery.fileupload-ui.js'
/ add include_tag js files to config.assets.precompile in ...environments/production.rb if you have it in vendor/ assets
%script{:charset => "utf-8", :type => "text/javascript"}
  $(function () {
  \// Initialize the jQuery File Upload widget:
  $('#fileupload').fileupload();
  \//
  \// Load existing files:
  $.getJSON($('#fileupload').prop('action'), function (files) {
  var fu = $('#fileupload').data('blueimp-fileupload'),
  template;
  fu._adjustMaxNumberOfFiles(-files.length);
  template = fu._renderDownload(files)
  \.appendTo($('#fileupload .files'));
  \// Force reflow:
  fu._reflow = fu._transition && template.length &&
  template[0].offsetWidth;
  template.addClass('in');
  $('#loading').remove();
  });

  });
4

1 回答 1

0

我也遇到了这样的问题,我必须正确地包含 Bootstrap CSS 代码。但是,我实际上并不希望在我的样式表中使用 Bootstrap 库,因为它很大并且会干扰我的其他样式,因此我必须进行一些试验并挑选出那些实际影响按钮的样式。

以下是我使用的一些样式,可以帮助您朝着正确的方向开始。(这可能无法解决所有问题)。

为此,我参考了官方演示页面: https ://blueimp.github.io/jQuery-File-Upload/

然后我打开了 Chrome 开发者工具并查看了正在应用的样式。提交文件的表单输入类型实际上隐藏在“添加文件”按钮中。这有点尴尬,但设计使然。该按钮是隐藏的,因为它仅用于保存文件的值,而表单是由插件的 JavaScript 提交的。所以我们想要对那个文件输入按钮做的position: absolute;就是让它不会扭曲其他页面元素。使用插件上传时,插件将使用 AJAX 而不是直接提交表单,之后不应重定向您。如果之后仍然无法正常工作,则可能是您服务器上的处理脚本配置错误。为了提供帮助,我需要更详细地查看代码。

.btn-success {
    color: #fff;
    background-color: #5cb85c;
    border-color: #4cae4c;
}
.btn {
    display: inline-block;
    padding: 6px 12px;
    margin-bottom: 0;
    font-size: 14px;
    font-weight: 400;
    line-height: 1.42857143;
    text-align: center;
    white-space: nowrap;
    vertical-align: middle;
    cursor: pointer;
    -webkit-user-select: none;
    -moz-user-select: none;
    -ms-user-select: none;
    user-select: none;
    background-image: none;
    border: 1px solid transparent;
    border-radius: 4px;
} 
.fileinput-button {
    position: relative;
    overflow: hidden;
    display: inline-block;
}
.fileinput-button input {
    position: absolute;
    top: 0;
    right: 0;
    margin: 0;
    opacity: 0;
    font-size: 200px;
    direction: ltr;
    cursor: pointer;
}
于 2017-04-08T16:00:45.407 回答