ajax中form表单中含有文件上传功能,后台语言是java,包括图片的下载,预览,删除
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ajax中form表单中含有文件上传功能,后台语言是java,包括图片的下载,预览,删除相关的知识,希望对你有一定的参考价值。
//样式
<li>
<label>已有文档:</label>
<div id="fileEdit"></div>
</li>
<li>
<label>附件:</label>
<input name="file" type="file" multiple="multiple" style="display: inline-block;"/><span id="spanContent" class="pull-right" style="color: royalblue;"></span>
</li>
<li style="text-align: center">
<button class="btn btn-sm btn-success" type="submit"><i class="fa fa-upload"></i> 提交</button>
<button class="btn btn-sm btn-danger" data-dismiss="modal"><i class="fa fa-times"></i> 取消</button>
</li>
//js
function addleave() { //onsubmit事件执行的函数;
var jsonuserinfo = $(‘#addleave‘).serializeJson();
var jsonuserinfodata = $(‘#addleave‘).serialize();
var attachmentdata = JSON.stringify(jsonuserinfo);
attachmentdata = $.base64.encode(attachmentdata, ‘utf8‘);
var data = new FormData($(‘#addleave‘)[0]);
$.ajax({
url : mainUrl+‘leave/add‘,
type : "post",
async:true,
data : new FormData($(‘#addleave‘)[0]),
contentType : "application/json;charset=utf-8",
dataType : "text",
processData: false,
contentType: false,
beforeSend: function(request) {
request.setRequestHeader("token", token);
request.setRequestHeader("attachmentdata", attachmentdata)
},
success : function show(result) {
if(result==‘success‘){
$(‘#addleave‘)[0].reset();
}else {
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
}
});
return false;
}
编辑中显示的文件
function edit(){ //点击编辑按钮的函数
if(mainid == ‘‘){
popWindow(‘未选中‘)
}else {
if(fileName==‘‘||fileName==null){
$(‘#fileEdit‘).html(‘无‘)
}else{
var name = fileName.split("|");
var url = fileUrl.split("|");
var suf = fileSuf.split("|");
var op=‘‘;
for(var i=0;i<name.length;i++){
if(suf[i]==‘png‘||suf[i]==‘jpeg‘||suf[i]==‘pdf‘||suf[i]==‘tif‘||suf[i]==‘gif‘){
op+="<div style=‘width:100%‘><span style=‘display:none‘>"+i+"</span>"+name[i]+
"<a class=‘pull-right m-l‘ onclick=‘delFile(this)‘>删除</a>"+
"<a class=‘pull-right m-l‘ href=‘"+mainUrl+"leave/filedown?id="+mainid+"&index="+i+"‘>下载</a>"+
"<a class=‘pull-right m-l‘ target=‘_blank‘ href=‘"+fileMeeting+url[i]+"‘>预览</a>"+
"<span class=‘clearfix‘></span></div>"
}else{
op+="<div style=‘width:100%‘><span style=‘display:none‘>"+i+"</span>"+name[i]+
"<a class=‘pull-right m-l‘ onclick=‘delFile(this)‘>删除</a>"+
"<a class=‘pull-right m-l‘ href=‘"+mainUrl+"leave/filedown?id="+mainid+"&index="+i+"‘>下载</a>"+
"<span class=‘clearfix‘></span></div>"
}
}
$(‘#fileEdit‘).html(op)
}
$(‘#myModal1‘).modal(‘show‘);
}
}
function delFile(org){ //点击删除执行的函数
$(org).parent().remove();
}
function doEdit(){ //编辑提交执行的函数
var ii=‘‘;
var length=$(‘#fileEdit‘).find(‘div‘).length;
for(var i=0;i<length;i++){
var t = $(‘#fileEdit‘).find(‘div‘).eq(i).find(‘span‘).html();
ii+=t+‘&‘;
}
var jsonuserinfo = $(‘#doEdit‘).serializeJson();
var jsonuserinfodata = $(‘#doEdit‘).serialize();
var attachmentdata = JSON.stringify(jsonuserinfo);
attachmentdata = $.base64.encode(attachmentdata, ‘utf8‘);
var data = new FormData($(‘#doEdit‘)[0]);
$(‘.spinnerBox‘).show();
$.ajax({
url : mainUrl+‘leave/update‘,
type: ‘POST‘,
cache: false,
data: new FormData($(‘#doEdit‘)[0]),
processData: false,
contentType: false,
dataType: ‘text‘,
beforeSend: function(request) {
request.setRequestHeader("token", token),
request.setRequestHeader("attachmentdata", attachmentdata),
request.setRequestHeader("attachmentindex", ii)
},
success : function show(result) {
if(result==‘success‘){
popWindow(‘编辑成功‘);
$(‘#myModal1‘).modal(‘hide‘);
$("#grid1").data(‘kendoGrid‘).dataSource.read();
$("#grid1").data(‘kendoGrid‘).refresh();
}else {
popWindow(‘编辑失败‘);
$(‘#myModal1‘).modal(‘hide‘);
}
},
error: function(XMLHttpRequest, textStatus, errorThrown) {
},
complete: function(XMLHttpRequest, textStatus){
$(‘.spinnerBox‘).hide();
}
});
return false;
}
</script>
以上是关于ajax中form表单中含有文件上传功能,后台语言是java,包括图片的下载,预览,删除的主要内容,如果未能解决你的问题,请参考以下文章
工作中如何使用ajax提交form表单,包括ajax文件上传
怎么用ajax 提交上传的文件上传后在界面显示出来。但界面不刷新
jquery.form.js提交 input file中的文件