在做图片上传功能的时候,遇到了JS无法识别图片type的问题,在使用过程中是没有问题的,但是不知道为什么浏览器的Console报这个错误:

Uncaught TypeError: Cannot read property 'type' of undefined
at Function.$.ImgSrc (ModelUpload.do:18)
at uploadImg (ModelUpload.do:77)
at HTMLInputElement.onchange (ModelUpload.do:110)


alert下面JS代码中的file[i].type的时候显示的是img/jpeg why???
页面相关代码如下:
HTML:

        <p>
<label for="picFileId">缩略图文件:</label> <input id="picFileId"
name="picFileId" type="file" onchange="uploadImg()" />
<fieldset>
<div style="position: relative;" id="fileImg"></div>
<legend>图片显示区域</legend>
</fieldset>
<p>

JS:

<script type="text/javascript">
$.ImgSrc = function(file, id) {
for (var i = 0; i < 3; i++) {
alert(file[i].type);
if (!/image\/\w+/.test(file[i].type)) { alert("请选择图片文件");
return false;
}
;
if (file[i].size > 2048 * 1024) {
alert("图片不能大于2M")
ClearImg();
continue;
}
;
var img;
console.log(document.getElementById("fileImg"));
console.log(file[i]);
console.log("file-size=" + file[i].size);
var reader = new FileReader();
reader.onloadstart = function(e) {
console.log("开始读取....");
}
reader.onprogress = function(e) {
console.log("正在读取中....");
}
reader.onabort = function(e) {
console.log("中断读取....");
}
reader.onerror = function(e) {
console.log("读取异常....");
}
reader.onload = function(e) {
console.log("成功读取....");
var div = document.createElement("div"); //外层 div
div
.setAttribute(
"style",
"position:relative;width:inherit;height:inherit;float:left;z-index:2;width:150px;margin-left:8px;margin-right:8px;");
var del = document.createElement("div"); //删除按钮div
del
.setAttribute(
"style",
"position: absolute; bottom: 4px; right: 0px; z-index: 99; width: 30px; height:30px;border-radius:50%;")
var delicon = document.createElement("img");
delicon.setAttribute("src", "images/shanchu.png");
delicon.setAttribute("title", "删除");
delicon.setAttribute("style",
"cursor:pointer;width: 30px; height:30px");
del.onclick = function() {
this.parentNode.parentNode.removeChild(this.parentElement);
ClearImg();
};
del.appendChild(delicon);
div.appendChild(del);
var imgs = document.createElement("img"); //上传的图片
imgs.setAttribute("name", "loadimgs");
imgs.setAttribute("src", e.target.result);
imgs.setAttribute("width", 150);
//childNodes.length > 0 代表 最多传一张,再上传,就把前面的替换掉
if (document.getElementById(id).childNodes.length > 0) {
document.getElementById(id).removeChild(
document.getElementById(id).firstChild);
}
div.appendChild(imgs)
document.getElementById(id).appendChild(div);
}
reader.readAsDataURL(file[i]);
}
}
function uploadImg() {
$.ImgSrc(document.getElementById("picFileId").files, "fileImg");
}
function ClearImg() {
var file = $("#picFileId")
file.after(file.clone().val(""));
file.remove();
}
</script>

JS报错:Cannot read property 'type' of undefined >> java

这个答案描述的挺清楚的:
http://www.goodpm.net/postreply/java/1010000008888543/JS报错Cannotreadpropertytypeofundefined.html

最新文章

  1. mvc ef LINQ to Entities 不识别方法“Int32 Parse(System.String)”,因此该方法无法转换为存储表达式。
  2. [vim]的关键字补全
  3. zz
  4. Android 动画 6问6答
  5. 打造万能的ListView GridView 适配器
  6. java解析XML四种方法
  7. 安森美电量计采用内部电阻跟踪电流--电压HG-CVR
  8. spark高级排序彻底解秘
  9. .NET的DTO映射工具AutoMapper
  10. js 如何判断鼠标点击事件还是js代码调用
  11. 为什么各大厂商要抢先跟进H.265?
  12. 转自知乎(JAVA后台开发可以纯粹用JAVA SE吗?)
  13. [asp.net core 源码分析] 01 - Session
  14. SSM框架中如何简便上传文件表单
  15. Linux目录/usr缩写及目录结构说明
  16. (网页)Java程序员们最常犯的10个错误(转)
  17. 静态HTML总结
  18. putty加了密钥ssh不能登陆,PuTTY:server refused our key问题的解决(转)
  19. asp.net core mvc 统一过滤参数,防止注入漏洞攻击
  20. UVa 12219 公共表达式消除

热门文章

  1. 基础apache命令
  2. 基于nginx的最基础的TCP代理,经过测试可通!
  3. SQlException 对象名无效
  4. SQL Server的复合索引学习【转载】
  5. Guitar Pro 的双十一特惠活动,正在如火如荼进行中...
  6. ZBrush中Document特性介绍
  7. Spring Batch 高级-
  8. [NOIP补坑计划]NOIP2017 题解&amp;做题心得
  9. HDU 1465 不容易系列之一( 错排水题 )
  10. debian 9 安装无线网卡