html5 上传图片并显示

<img id="my-image" src=''>
<input  type="file" capture="camera" accept="image/*" id="cameraInput0" name="cameraInput" οnchange="chooseImage(this);"/>
var datas = {
};
/*<span style="white-space:pre">	</span>inputfile  onchange事件、
 *<span style="white-space:pre">	</span>获取图片数据,并将他显示到页面
 */
<pre name="code" class="html">function chooseImage(<span style="font-family: Arial, Helvetica, sans-serif;">files</span><span style="font-family: Arial, Helvetica, sans-serif;">){</span><pre name="code" class="html"><span style="white-space:pre">	</span>datas<span style="font-family: Arial, Helvetica, sans-serif;">.imageObject = files;</span>
files=files.files;
for (var i = 0; i < files.length; i++) {
 
<span style="white-space:pre">		var file = files[i];
<span style="white-space:pre">		</span>var reader = new FileReader();
<span style="white-space:pre">	</span>        reader.onload = function (e) {</span>
<span style="white-space:pre">			$("</span>#<span style="font-family: Arial, Helvetica, sans-serif;">my-image</span><span style="font-family: Arial, Helvetica, sans-serif;">").attr("src",this.result);</span>
<span style="white-space:pre"><span style="white-space:pre">	</span>        }
<span style="white-space:pre">	</span>        reader.readAsDataURL(file);</span>
<span style="white-space:pre">	</span>}
}
/*
 * <span style="white-space:pre">	</span>上传
 */
function upload(){
<span style="white-space:pre">	var formData = new FormData();</span>
<span style="white-space:pre">	</span>var username = '';
<span style="white-space:pre">	formData.append('</span>file<span style="white-space:pre">',</span>datas<span style="white-space:pre">.imageObject.files[0]);	</span>//图片数据
<span style="white-space:pre">	formData.append(</span>'<span style="font-family: Arial, Helvetica, sans-serif;">username</span><span style="font-family: Arial, Helvetica, sans-serif;">',</span>'13574747474'<span style="font-family: Arial, Helvetica, sans-serif;">);				</span>//存放字段
<span style="white-space:pre">	var xhr = new XMLHttpRequest();</span>
<span style="white-space:pre">	</span>var url =''<span style="white-space:pre">	</span>//上传路径
<span style="white-space:pre">	xhr.open('POST',</span>url<span style="white-space:pre">,true);	</span>
<span style="white-space:pre">	xhr.send(formData);</span>
}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值