jquery 文件上传 触发两次,JQuery文件上传:在data.submit()上发送两个请求

I am trying to Upload videos in my app using Paperclip and jquery-fileupload-rails

I followed ulpoad file with paper clip and jquery to upload video, but was uploading video just as I select the video and don't wait for pressing submit button, So I followed

1: jQuery file upload: Is it possible to trigger upload with a submit button? to select a single video and upload it on submit button, not on selecting file.

But There is issue on whether I put submit button in the form or outside form.

When I put the submit button in the form than it sends two requests to pressing submit button, one with Video and second one without Video.

And If I put button outside form that it sends one request on pressing submit button but after successful create/update it remains on the same page.

All I wanted is that on pressing Submit, sending just one request and redirect to index page on successful create/update.

In _form.slim

= simple_form_for [:admin,add] do |f|

.wizard-content

.row

.col-sm-6

= f.input :video, multiple: false, wrapper: :horizontal_file_input

button#submit_button.btn.btn-primary

| Save

In adds.coffee

jQuery ->

$('#add_video').attr('name','add[video]')

$('#add_video').fileupload

$('#submit_button').off('click').on 'click', ->

data.submit()

In controller I have

def create

if add.save

redirect_to admin_adds_path

flash[:success]= "Add Created"

else

render :new

end

end

def update

if add.update(add_params)

redirect_to admin_adds_path

flash[:success]= "Add Updated"

else

render :edit

end

end

there is also a _add.slim

= image_tag(@add.uploaded_file(:small), class: 'thumb')

I also created create.js.erb and edit.js.erb as suggested tutorial I followed (though, I didn't understand its use )

- if @add.new_record?

| alert('Failed');

- else

| if ($('h1') !== undefined) { $('h1').append("

=j render partial: 'adds/add', locals: { add: @add }

| "); }

Whats wrong with this?

解决方案

When you put button outside form you can handle the done event and the redirect.

Remember this POST is ajax and if you POST a change or new element you have a response via ajax no via web browser page.

Some like

$('#fileupload').fileupload({

dataType: 'json',

add: function (e, data) {

$("#up_btn").off('click').on('click', function () {

data.submit();

});

done: function (e, data) {

window.location.href = "data.next_url";

}

},

});

Maybe you need to response some json (instead of redirection redirect_to admin_adds_path) to pass from controller to javascript information (some like success,error,next_url,others)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值