ajax 提交form表单的 文件上传

搞文件上传,要么使用 文件上传控件! 先异步上传之后获取上传之后的文件内容,最后再把 文件信息和其他相关内容一起提交给后台。

要么就是一个 form表单 ,把文件和其他信息一把都传向后台! 

但是form表单上传之后, 往往都是会跳转页面的。

所以就想要 可以ajax 上传文件。

百度了一下,没想到还真的有:

可以参考

https://www.cnblogs.com/LoveTX/p/7081515.html

var fd = new FormData();
    fd.append('id', $("#id").val());
    //fd.append('sealPicPathFile', $("#sealPicPathFile").val()); 不可以这样
	fd.append('sealPicPathFile', document.getElementById("sealPicPathFile").files[0]);
	fd.append('limitAmount', $("#limitAmount").val());
	fd.append('orderNum', $("#orderNum").val());
	fd.append('type', $("#type").val());
	
	$.ajax({
		url:'/admin/contract/guaranteecompany/save',
		data: fd ,
		type:'post',
		dataType: 'json',
		processData:false,  //tell jQuery not to process the data
        contentType: false,  //tell jQuery not to set contentType
		success:function(request){
			console.log(JSON.stringify(request));
			if(request.message == "OK"){
				 alertMsg.correct("保存成功");
				// $("#btnBack").click();
			}else{
				 alertMsg.info("保存失败," + request.message);
			}
		}
	});

后台代码就想 之前 使用 form表单 提交一样的。不需要改动

猜你喜欢

转载自my.oschina.net/u/2419285/blog/2981034