进度条不会在文件上传后删除,jquery文件上传

时间:2013-06-04 11:44:09

标签: ruby-on-rails-3 jquery-file-upload

我正在使用jquery-fileupload-rails Gem在我的应用程序中上传图片。 这是代码:

<script id="template-upload" type="text/x-tmpl">
  <div class="upload ">
  {%=o.name%}
  <div class="progress"><div class="bar" style="width: 0%"></div></div>

<div class="photo-post-img">
<%= form_for Upload.new do |f| %>
  <span class="btn btn-primary fileinput-button ml30">
    <b>+</b>
    <span class="text-orange">Add image</span>
    <%= f.file_field :upload, multiple: true, name: "upload[upload]" %>
    <%= f.hidden_field :post_id, :value => @post.upload_id %>
    <%= f.hidden_field :upload_type, :value => @post.upload_type %>
  </span>
<% end %>
<div class="img-upload-list">
  <ul id="photos_uploaded" class="thumbnails pull-left">
    <% @post.uploads.each do |upload| %>
      <li class="thumbnail" id="uploads-<%=upload.id %>">
        <%=image_tag upload.upload.url(:medium) %>
        <%= link_to "Delete", upload, :remote => true, :method => "delete" %>
      </li>
     <% end %>
  </ul>
</div>

在.js文件中:

jQuery(function() {
  return $('#new_upload').fileupload({
  dataType: "script",
  add: function(e, data) {
    var file, types;
    types = /(\.|\/)(gif|jpe?g|png)$/i;
    file = data.files[0];
    if (types.test(file.type) || types.test(file.name)) {
      data.context = $(tmpl("template-upload", file));
      $('#new_upload').append(data.context);
      return data.submit();
    } else {
       return alert("" + file.name + " is not a gif, jpeg, or png image file");
    }
   },

   progress: function(e, data) {
     var progress;
     if (data.context) {
       progress = parseInt(data.loaded / data.total * 100, 10);
       return data.context.find('.bar').css('width', progress + '%');
      }
    },
   done: function (e, data) {
     $('.upload').attr('style', 'visibility: hidden');
   }
 });
});

每件事都可以正常工作,但在图片上传后,进度条不会删除。如何在上传后删除栏?

1 个答案:

答案 0 :(得分:4)

而不是done回调在js文件中添加stop回调

stop: function (e, data) {
  $('.upload').hide();
},