Js实现文件上传无刷新以及图片预览
2016-07-18 09:16
781 查看
JS想实现无刷新文件上传,还得使用iframe和form,通过在页面内放入一个内置form表单的iframe,让文件在上传的时候只发生局部刷新。
具体实现步骤:
1.首先在页面里定义iframe和form:
<body>
//自己的项目代码
//iframe随便定义在什么位置
<iframe name="uploadfrag" src="src/components/uploadfragment.html" frameborder="0" scrolling="no" width="100%" height="100%"></iframe>
</body>
2.在uploadfragment.html中定义form表单和上传图片函数并实现上传预览
<body>
//注意enctype必写,上传文件的表单必须使用该格式
<form id="uploadform" action="上传的地址" enctype="multipart/form-data" method="post">
<input id="uploadimg" type="file" name="image"/>
<input type="hidden" name="要上传参数的字段名" value="要上传的参数的值"/>
<button class="upload-img-submit" type="submit" onclick="upload()">确定</button>
</form>
</form>
<script>
var uli = document.getElementById("uli")
if(uli.addEventListener){
uli.addEventListener("change",function(){
var url = null ;
var file = document.getElementById("uli").files[0]
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
var img = document.createElement("img")
img.src = url
img.width = 200
img.height = 360
img.style.border = "1px solid #333"
img.style.marginTop = "15px"
document.getElementById("uploadform").appendChild(img)
})
}else{
uli.attachEvent("onChange",function(){
var url = null ;
var file = document.getElementById("uli").files[0]
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
var img = document.createElement("img")
img.src = url
img.width = 200
img.height = 360
img.style.border = "1px solid #333"
img.st
4000
yle.marginTop = "15px"
document.getElementById("uploadform").appendChild(img)
})
}
</script>
</body>
具体实现步骤:
1.首先在页面里定义iframe和form:
<body>
//自己的项目代码
//iframe随便定义在什么位置
<iframe name="uploadfrag" src="src/components/uploadfragment.html" frameborder="0" scrolling="no" width="100%" height="100%"></iframe>
</body>
2.在uploadfragment.html中定义form表单和上传图片函数并实现上传预览
<body>
//注意enctype必写,上传文件的表单必须使用该格式
<form id="uploadform" action="上传的地址" enctype="multipart/form-data" method="post">
<input id="uploadimg" type="file" name="image"/>
<input type="hidden" name="要上传参数的字段名" value="要上传的参数的值"/>
<button class="upload-img-submit" type="submit" onclick="upload()">确定</button>
</form>
</form>
<script>
var uli = document.getElementById("uli")
if(uli.addEventListener){
uli.addEventListener("change",function(){
var url = null ;
var file = document.getElementById("uli").files[0]
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
var img = document.createElement("img")
img.src = url
img.width = 200
img.height = 360
img.style.border = "1px solid #333"
img.style.marginTop = "15px"
document.getElementById("uploadform").appendChild(img)
})
}else{
uli.attachEvent("onChange",function(){
var url = null ;
var file = document.getElementById("uli").files[0]
if (window.createObjectURL!=undefined) { // basic
url = window.createObjectURL(file) ;
} else if (window.URL!=undefined) { // mozilla(firefox)
url = window.URL.createObjectURL(file) ;
} else if (window.webkitURL!=undefined) { // webkit or chrome
url = window.webkitURL.createObjectURL(file) ;
}
var img = document.createElement("img")
img.src = url
img.width = 200
img.height = 360
img.style.border = "1px solid #333"
img.st
4000
yle.marginTop = "15px"
document.getElementById("uploadform").appendChild(img)
})
}
</script>
</body>
相关文章推荐
- JS 根据今天的日期获取本周星期一与星期天的日期
- [学习笔记]javascript的语法和关键字
- Javascript对象继承
- 深入理解JS正则表达式---分组
- 在Javascript中onclick()方法应用
- 简单的JS轮播图代码
- JavaScript获取css行间样式,内连样式和外链样式的简单方法
- Javascript—String类型
- js鼠标事件中的clientX/clientY,screenX/screenY,pageX/pageY,offsetX/offsetY,layerX/layerY,x/y
- simplejson模块介绍
- javascript对象,函数,原型链
- javascript面向对象学习笔记(五)——模块化
- javascript面向对象学习笔记(四)——常用方法/属性
- javascript面向对象学习笔记(三)——继承
- 超实用的JavaScript代码段 Item7 -- “&&”和“||”操作符总结
- 老生常谈IE兼容性:ie6 javascript:void(0)、IE7 input透明、IE8 j
- $.parseJSON 报错 解决方法
- js仿QQ中对联系人向左滑动、滑出删除按钮的操作
- JS延时执行函数
- javascript作用域