function upload(input) {
//支持chrome IE10
if (window.FileReader) {
var file = input.files[0];
filename = file.name.split(".")[0];
var reader = new FileReader();
reader.onload = function() {
console.log(this.result)
console.log(file)
// alert(this.result);
}
reader.readAsText(file);
}
//支持IE 7 8 9 10
else if (typeof window.ActiveXObject != 'undefined'){
var xmlDoc;
xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async = false;
xmlDoc.load(input.value);
console.log(input)
// alert(xmlDoc.xml);
}
//支持FF
else if (document.implementation && document.implementation.createDocument) {
var xmlDoc;
xmlDoc = document.implementation.createDocument("", "", null);
xmlDoc.async = false;
xmlDoc.load(input.value);
console.log(input)
// alert(xmlDoc.xml);
} else {
alert('error');
}
}
let myinput = `<input type='file' onchange='upload(this)' />`
document.body.innerHTML=myinput
浏览器上传兼容性处理 可以扩展上传文件类型限制 也可以扩展上传文件大小限制 |