首页 > 代码库 > ajax上传图片的方法

ajax上传图片的方法

	

	$("#fileUp").change(function() {
			event.preventDefault()
			let files = event.target.files;

			const myfiles = files
			let i
			for(i = 0; i < files.length; i++) {
				files[i].thumb = URL.createObjectURL(files[i])
			}
			files = Array.prototype.slice.call(files, 0)
			files = files.filter(function(file) {
				return /image/i.test(file.type)
			})
			$(".upload-iconup-logo-pic").attr("src", (files[0].thumb));
			save();
		})

		function save() {
			$(‘#formFile‘).attr("action","ip/index.php?act=sign_active&op=upImg&unionid="+unionid);
			var form = new FormData(document.getElementById("formFile"));
			$.ajax({
				url: "ip/index.php?act=sign_active&op=upImg&unionid="+unionid
				type: "post",
				data: form,
				cache: false,
				processData: false,
				contentType: false,
				success: function(data) {
					const obj = JSON.parse(data);
					console.log(data)
				},
				error: function(e) {
					alert("网络错误,请重试!!");
				}
			});
		}

ajax上传图片的方法