要进行文件的上传,需要对form表单进行特殊设置;
1.设定表单数据的提交方式为POST
2.设定enctype属性值为:multipart/form-data
<form action="php地址" method="post" enctype="multipart/form-data">
<input type="file" name="fs" id="doc"> //file必须
<input type="submit">
</form>
//打印上传图片信息
<div id="info"> </div>
//打印图片
<img src="" id="dis">
js:
var doc = document.getElementById('doc');
var info = document.getElementById('info');
var dis = document.getElementById('dis');
doc.onchange = function(){
var file = this.files[0];
var html = "name:"+file.name+"<br>";
html += "size:"+file.size+"<br>";
html += "type:"+file.type;
info.innerHTML = html;
//URL.createObjectURL()方法会根据传入的参数创建一个指向该参数对象的URL.
var url = URL.createObjectURL(file);
dis.src = url;
}