原生js实现ajax的文件异步提交功能、图片预览功能.实例
采用html5使得选择图片改变时,预览框中图片随之改变。input文件选择框美化。原生js完成文件异步提交
效果图:
代码如下,可直接复制并保存为html文件打开查看效果
- <html>
- <head>
- <title>Title</title>
- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css">
- <script src="http://apps.bdimg.com/libs/jquery/2.1.4/jquery.min.js"></script>
- <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js"></script>
- <script>
- // html5实现图片预览功能
- $(function(){
- $("#file").change(function(e){
- var file = e.target.files[0]||e.dataTransfer.files[0];
- $('#photoCover').val(document.getElementById("file").files[0].name);
- if(file){
- var reader = new FileReader();
- reader.onload=function(){
- $("img").attr("src", this.result);
- }
- reader.readAsDataURL(file);
- }
- });
- })
- function saveUser(){
- var id = $("#id").val().trim();
- var uname = $("#uname").val().trim();
- var pwd = $("#pwd").val().trim();
- var file = document.getElementById("file").files[0];
- // 原生ajax实现文件上传
- var form = new FormData();
- form.append("uname", uname); // 可以增加表单数据
- form.append("id", id);
- form.append("pwd", pwd);
- if(file){
- form.append("file", file);
- }
- var xhr = null; //得到xhr对象
- if(XMLHttpRequest){
- xhr = new XMLHttpRequest();
- }else{
- xhr = new ActiveXObject("Microsoft.XMLHTTP");
- }
- xhr.open("post", "${ctx}/user/save", true);//设置提交方式,url,异步提交
- xhr.onload = function ()
- {
- var data = xhr.responseText; //得到返回值
- // alert(data);
- var errorMsg = JSON.parse(data);
- alert(errorMsg.msg);
- if(errorMsg.code == "0"){
- alert("success"); //成功
- }
- }
- xhr.send(form);
- }
- </script>
- </head>
- <body style="overflow:scroll;overflow-y:hidden;overflow-x:hidden">
- <div style="height: 20px"></div>
- <div class="container">
- <div class="row">
- <div class="col-md-6 col-md-offset-3">
- <form class="form-horizontal" enctype="multipart/form-data" role="form">
- <input type="hidden" value="${user.id}" id="id"/>
- <div class="control-group">
- <label for="uname" class="col-md-3 control-label span3">姓 名:</label>
- <div class="col-md-9">
- <input type="text" class="form-control span3" value="" id="uname"
- placeholder="请输入姓名">
- </div>
- </div>
- <div class="control-group">
- <label for="pwd" class="col-md-3 control-label span3">密码:</label>
- <div class="col-md-9">
- <input type="password" class="form-control span3" value="" id="pwd"
- placeholder="请输入密码">
- </div>
- </div>
- <div class="control-group">
- <label class="col-md-3 control-label span3"></label>
- <div class="col-md-9">
- <img src="" width="100px" height="100px">
- </div>
- </div>
- <div class="control-group">
- <label for="requirement" class="col-md-3 control-label span3">图片上传</label>
- <div class="col-md-9">
- <div class="input-group">
- <input id="photoCover" class="form-control" readonly type="text">
- <label class="input-group-btn">
- <input id="file" type="file" style="left:-9999px;position:absolute;">
- <span class="btn btn-default">Browse</span>
- </label>
- </div>
- </div>
- </div>
- <div class="control-group">
- <label class="col-md-2 control-label span2"></label>
- <div class="col-md-10">
- <button type="button" class="btn btn-small btn-primary" onclick="saveUser()">提交</button>
- <a type="button" class="btn btn-small btn-danger">取消</a>
- </div>
- </div>
- </form>
- </div>
- </div>
- </div>
- </body>
- </html>
原生js实现ajax的文件异步提交功能、图片预览功能.实例的更多相关文章
- js实现form表单提交,图片预览功能
代码如下 <html> <head> <meta http-equiv="Content-Type" content="text/html; ...
- 原生JS实现图片预览功能
html代码: <div class="album-new fr"> <div class="upload-btn btn-new container& ...
- 如何通过js实现图片预览功能
一.效果预览 效果图: 二.实现代码: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" &quo ...
- 34)django-上传文件,图片预览功能实现
目录 文件上传 1)form表单提交上传(会刷新) 2)ajax上传 3)iframe 4)图片上传预览(思路保存文件的时候,把文件保存文件的路径反馈回,客户端 ...
- dwz+jquery+fileupload+springmvc实现文件上传 及图片预览
1 前台jsp:文件的上传利用了iframe实现局部刷新功能.使用了apache的fileupload组件,用到的jar: commons-fileupload.jar,commons-io.jarD ...
- 通过file文件选择图片预览功能
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- JS实现的图片预览功能
之前的博文有实现过图片上传预览,但那种方法是预览时就将图片上传,会产生很大的浪费空间.找到了之前有人写的用JS实现的图片预览,就说用js将上传的图片显示,上传代码在之前的博文中有写到. 以下是实现的代 ...
- javascript实现文件上传之前的预览功能
1.首先要给上传文件表单控件和图片控件设置name属性 <div class="form-group"> <label fo ...
- 利用js加载本地图片预览功能
直接上代码: 经测试,除safari6包括6以下不支持,其他均可正常显示. 原因:safari6不支持filereader,同时不能使用IE滤镜导致失效. fix: 可以利用canvas,解决safa ...
随机推荐
- Java开发环境的搭建(jdk,eclipse)
一.java 开发环境的搭建 这里主要说的是在windows 环境下怎么配置环境. 1.首先安装JDK java的sdk简称JDK ,去其官方网站下载最近的JDK即可. http://www.orac ...
- 170509、文本编辑器编写的shell脚本在linux下无法执行的解决方法
今天碰到一个奇怪的问题,编写好的shell脚本再linux上执行一直提示找不到文件或目录,后来想想是文本编辑器的问题,记录下来!!! 1.查看当前文本格式 Notepad++界面中,在右下角有文件格式 ...
- 新增form表单,post提交.2
- Quartz学习记录
参考资料: 官方网站 Quartz使用总结
- vsftpd文件服务器安装与配置
-d<登入目录>:指定用户登入时的启始目录:. -s<shell>:指定用户登入后所使用的shell: /sbin/nologin指的是不允许login当前Linux系统.当用 ...
- form表单上传图片问题:线下可以而线上不可以
由于上传图片需要一定时间,而线下速度快线上速度慢. 所以如果你的上传窗口是弹出界面,那么就会面临上传未完成就关闭了该界面.导致上传失败.
- MySQL 的mysqldump备份
MySQL 的mysqldump备份 来自<mysql技术内幕 innodb存储引擎> --single-transaction:只对innodb表有效 --lock-tables:对My ...
- Underscore.js (1.7.0)-集合(Collections)(25)
稽核函数(数组或对象) each_.each(list, iteratee, [context]) 别名: forEach 遍历list中的所有元素,按顺序用遍历输出每个元素.如果传递了context ...
- [py]class的特殊方法
类方法 解释 hasattr hasattr(class) getattr - setattr - delattr - - - __getattr__ __setattr__ __delattr__ ...
- Virtualbox中win7虚拟机中U盘不可用问题的解决
Virtualbox版本是5.0.0,主机运行多是Ubuntu12.04 LTS,虚拟机是Win7 X64.起初Win7正常运行,Virtualbox的增强功能已安装.下面是如何一步一步解决U盘不可用 ...