通过ajax jquery不带表单上传多个文件
我有一个对话框,用户可以选择上传文件。我想单击jquery对话框中的按钮将文件上载到服务器。我尝试了许多代码,但不幸的是,我找不到解决方案。请帮助我 我的小提琴 Jquery:通过ajax jquery不带表单上传多个文件,ajax,file-upload,jquery-ui-dialog,Ajax,File Upload,Jquery Ui Dialog,我有一个对话框,用户可以选择上传文件。我想单击jquery对话框中的按钮将文件上载到服务器。我尝试了许多代码,但不幸的是,我找不到解决方案。请帮助我 我的小提琴 Jquery: $(function(){ $(document).on('click','.click_link',function(){ $("#dialog_loader").dialog("open"); $("#dialog_loader").css({'display':'show'}); return false;
$(function(){
$(document).on('click','.click_link',function(){
$("#dialog_loader").dialog("open");
$("#dialog_loader").css({'display':'show'});
return false;
});
$("#dialog_loader").dialog({resizable: false,
height:"auto",
modal: true,
minWidth: 400,
autoOpen:false,
position: 'center top',
buttons: {
"Update": function() {
var form_data = new FormData();
$.each($("input[type='file']")[0].files, function(i, file) {
form_data.append('file', file);
});
form_data.append("status", 'update');
$.ajax({
url:path,
type:'POST',
dataType: "HTML",
contentType : false,
processData : false,
data:form_data,
success:function(msg){
if(msg==1)
{
alert(123);
}
}
});
},
"Approve":function(){
},
Cancel: function() {
$( this ).dialog( "close" );
}
}
});
$(document).on('click','.add_more',function(e){
e.preventDefault();
var filePresent = document.getElementsByClassName('file')[document.getElementsByClassName('file').length-1].files.length;
if(filePresent >0 ){
$(this).parent().find('#extra_file_div').find('.file_div_child').append("<br/><input name='file_uploads[]' type='file' class='multi_files file' /><button class='remove'>X</button>");
//$(this).before("<div class='file_div_child'><input name='file_uploads[]' type='file' class='multi_files file' /><button class='remove'>X</button></div>");
}
});
$(document).on('change','input:file',
function(){
$('input:file').removeClass('multi_files');
if ($(this).val()) {
if($(this).parent().parent().find('.remove').length <1)
{
$(this).after("<button class='remove first_remove' >X</button>");
}
$('.add_more').show();
}
else
{
$('.add_more').hide();
}
});
$(document).on('click','.remove',function(){
var length = $('#dialog_attachments').find(".file").length;
if(length > 1 ){
$(this).prev('input:file').remove();
$(this).prev('br').remove();
$(this).remove();
}
else
{
$(".file").val('');
$(this).remove();
$(this).parent('.file_div_child').find('br').remove();
$('.add_more').hide();
}
return false;
});
});
$(函数(){
$(文档)。在('单击','上。单击链接',函数(){
$(“对话框加载程序”)。对话框(“打开”);
$(“#dialog_loader”).css({'display':'show'});
返回false;
});
$(“#dialog_loader”).dialog({可调整大小:false,
高度:“自动”,
莫代尔:是的,
最小宽度:400,
自动打开:错误,
位置:'中间顶部',
按钮:{
“更新”:函数(){
var form_data=new FormData();
$.each($(“输入[type='file'])[0]。文件,函数(i,文件){
表单_data.append('file',file);
});
表格_data.append(“状态”,“更新”);
$.ajax({
url:path,
类型:'POST',
数据类型:“HTML”,
contentType:false,
processData:false,
数据:表格数据,
成功:功能(msg){
如果(msg==1)
{
警报(123);
}
}
});
},
“批准”:功能(){
},
取消:函数(){
$(此).dialog(“关闭”);
}
}
});
$(文档)。在('单击','上。添加更多',函数(e){
e、 预防默认值();
var filePresent=document.getElementsByClassName('file')[document.getElementsByClassName('file').length-1].files.length;
如果(文件存在>0){
$(this.parent().find(“#extra_file_div”).find(“.file_div_child”).append(“
X”);
//美元(本)。在“X”之前;
}
});
$(文档).on('change','input:file',
函数(){
$('input:file').removeClass('multi_files');
if($(this.val()){
if($(this.parent().parent().find('.remove')。长度1){
$(this.prev('input:file').remove();
$(this.prev('br').remove();
$(this.remove();
}
其他的
{
$(“.file”).val(“”);
$(this.remove();
$(this).parent('.file\u div\u child').find('br').remove();
$('.add_more').hide();
}
返回false;
});
});
我创建了一个表单,并将文件输入部分放在该表单元素中。
然后使用jquery的每个函数,我添加了每个文件输入,以形成_数据对象
var form_data = new FormData();
var inputs = $(document).find("#file_form input");
$.each(inputs, function (obj, v) {
$.each($(v)[0].files, function(i, file) {
form_data.append(v.name, file);
});
});
form_data.append("status", 'update');
我已经更新了小提琴。我已经创建了一个表单,并将文件输入部分放在该表单元素中。 然后使用jquery的每个函数,我添加了每个文件输入,以形成_数据对象
var form_data = new FormData();
var inputs = $(document).find("#file_form input");
$.each(inputs, function (obj, v) {
$.each($(v)[0].files, function(i, file) {
form_data.append(v.name, file);
});
});
form_data.append("status", 'update');
我已经更新了小提琴