上传图片AJAX(DemoText)

网页:

 <!--头像-->
            <div class="rs_content_headPortrait">
	                <span class="same">我的头像:</span>
	                
	                <c:if test="${useri.image==null}">
	               	 	<img src="../images/personage/touxiang.png" alt="" id="icon" width="50px" height="50px"/>
	                </c:if>
	                
	                <c:if test="${useri.image!=null }">
		                <img src="..${useri.image}" alt="" id="icon" width="50px" height="50px"/>
	                </c:if>
	    
	                <input type="file" name="file" id="iconPic" onchange="getImage(this)"/>
	                <!-- 
	                <span class="change_headPortrait same_click" data-toggle="modal" data-target="#avatar-modal" >更改头像</span>
	                 -->
	            </div>

js:

        //创建一个设置表单数据的对象
		var formData = new FormData();
		
		//获取上传文件的文件对象
		var file = document.getElementById("iconPic").files[0];//js 读取文件对象
        //formData.append('file', $('#input_file')[0].files[0]);  /*添加图片信息的参数*/		
		
		formData.append("file",file);//加入文件对象
		
		var param=formData;

$.ajax({
			url:'../user/upload.shop',
			type:'POST',
			cache:false,//不缓存上传文件
			data:param,
            //不处理数据:processData、contentType
			processData:false,
			contentType:false,
			
            success:function(obj){
				alert(obj.message);
				console.log(obj);
				//创建url对象
				var url = window.URL.createObjectURL(file);
				icon.src=url;
				
			},
			error:function(){
				alert("上传失败");
			}
			
			
		});

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值