您好,欢迎来到三六零分类信息网!老站,搜索引擎当天收录,欢迎发信息
免费发信息
三六零分类信息网 > 南昌分类信息网,免费分类信息发布

JavaScript实现文件异步上传的方法

2024/3/13 7:31:22发布16次查看
下面小编就为大家带来一篇原生javascript实现文件异步上传的实例讲解。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
效果图:
代码:(demo33.jsp)
<%@ page contenttype="text/html;charset=utf-8" language="java" %> <html> <head> <title>demo33.jsp</title> </head> <body> <label for="text">名称</label> <input type="text" id="text" name="name"/> <label for="file">文件</label> <input type="file" id="file" name="file"/> <button type="button" onclick="ajaxuploadfile()">确定</button> </body> <script type="text/javascript"> function ajaxuploadfile() { var formdata = new formdata(); var xmlhttp; if (window.xmlhttprequest) {// code for ie7+, firefox, chrome, opera, safari xmlhttp = new xmlhttprequest(); }else {// code for ie6, ie5 xmlhttp = new activexobject("microsoft.xmlhttp"); } xmlhttp.open("post","/data",true); xmlhttp.setrequestheader("x-requested-with", "xmlhttprequest"); formdata.append("name",document.getelementbyid("text").value); formdata.append("file",document.getelementbyid("file").files[0]); xmlhttp.send(formdata); xmlhttp.onreadystatechange=function() { if (xmlhttp.readystate==4) { if (xmlhttp.status==200) { console.log("上传成功"+xmlhttp.responsetext); }else { console.log("上传失败"+xmlhttp.responsetext); } } } } </script> </html>
以上就是javascript实现文件异步上传的方法的详细内容。
南昌分类信息网,免费分类信息发布

VIP推荐

免费发布信息,免费发布B2B信息网站平台 - 三六零分类信息网 沪ICP备09012988号-2
企业名录