ajax提交form表单上传附件

Html代码

	    <div class="import">
			<form id= "uploadForm" enctype="multipart/form-data">  
			  <div class="import_title">上传</div>
			  <div class="form-group">
				<div class="col-md-3" style="width:80%;float:left">
				  <input id="uploadFile" name="share_img" value ="" class="form-control"   readonly="true" />
				</div>
				<div class="col-md-3" style="width:20%;float:left;">
				  <div class="fileUpload btn01 btn-primary"> <span>选择文件</span>
					<input id="uploadBtn" type="file" name="file" class="upload" />
				  </div>
				</div>
			  </div>
			</form>
			  <div class="clear"></div>
			  <div class="import_submit" οnclick="doUpload();">订单导入</div>
	    </div>

js代码

function doUpload() {
	var formData = new FormData($( "#uploadForm" )[0]);  
	var file = $(":file").val();
	if(file){ 
		$.ajax({
			url: '/rest/insertInfo' ,  
			type: 'POST',  
			data: formData,   
			cache: false,  
			contentType: false,  
			processData: false,  
			beforeSend: function(){
				$("#loading").show(); //在后台返回success之前显示loading图标 
			},
			success: function(response) { 
				layer.msg(response.message);
				var result = response.result;
				var ahtml = "";
				$("#orders").empty();
				if(response.code==200){
					$.each(result,function(index,item){
						ahtml += '<tr><td>'+item.sizeContactPhone+'</td><td>'+item.siteName+'</td></tr>';
					})
					ahtml += '<tr><td>'+item.sizeContactPhone+'</td><td>'+item.siteName+'</td></tr>';
					$(".import1").show();
					$("#orders").append(ahtml);
				}
			}, 
			complete: function() {
				$("#loading").hide();
			},
			error: function(returndata) {   
				$("#loading").hide();
				layer.msg(response.message);
			}  
		});
	}else{
		layer.msg("请选择上传文件!");
	}
}


©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页