新手问题 使用 jQuery File Upload 上传多个图片的问题

yehualiu · 2014年03月17日 · 最后由 yehualiu 回复于 2014年03月18日 · 3452 次阅读

在 model 层面是 photos belongs_to film

/*这个是 photo 的 new.html.erb */

<%= form_for [:panel, @film, @photo], :remote => true do %>
<input id="fileupload" type="file" name="photo[photo_file]" multiple>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>
<script src="/jquery-file-upload/vendor/jquery.ui.widget.js"></script>
<script src="/jquery-file-upload/jquery.iframe-transport.js"></script>
<script src="/jquery-file-upload/jquery.fileupload.js"></script>
<script>
  $(function () {
    $('#fileupload').fileupload({
      dataType: 'json',
      url: '<%= panel_film_photos_path(@film.id) %>',
      done: function (e, data) {
        $.each(data.result, function (index, file) {
          $('<li><img src="'+file.thumbnail_url+'"></li>').appendTo('#thumbs');
        });
      }
    });
  });
</script>

<ul id="thumbs" class="cf">
<!-- json-ajax response here, see fileupload script above, .appendTo css id:"thumbs"-->
</ul>
<% end %>

/*这是 photo 的 controller */

def new
    @photo = Photo.new
    @film = Film.find(params["film_id"])
  end

  def create
    @photo = @film.photos.new(photo_params)
    respond_to do |format|
      if @photo.save

      else
        format.json {render json: @photo.errors, status: :unprocessable_entity}
      end
    end
  end

我这里上传运行不成功,望大牛指点

log 呢?

@miclle

Started GET "/panel/films/1/photos/new" for 127.0.0.1 at 2014-03-16 22:52:45 -0700
Processing by Panel::PhotosController#new as HTML
  Parameters: {"film_id"=>"1"}
  [1m[35mFilm Load (0.2ms)[0m  SELECT "films".* FROM "films" WHERE "films"."id" = ? ORDER BY created_at DESC LIMIT 1  [["id", "1"]]
  Rendered panel/photos/new.html.erb within layouts/application (6.8ms)
Completed 200 OK in 138ms (Views: 54.5ms | ActiveRecord: 11.4ms)

没有 post 提交

还有 create.json.builder 代码为

json.array!([@photo]) do |json, photo|
  json.name              photo.photo_file
  json.size              photo.photo_file.size
  json.current_path     photo.photo_file.current_path
  json.url               photo.photo_file.url
  json.thumbnail_url   photo.photo_file.url(:thumb)
end

没有 post 提交,那问题肯定是前端那边的 好像没看到表单提交按钮或者是类似的触发提交的代码..

:html => { :multipart => true} 貌似没有吧

@congteng jQuery File Upload 这个模块会自动处理的嘛

需要 登录 后方可回复, 如果你还没有账号请 注册新账号