纯js上传文件 很好用
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>参数设置</title>
<meta name="keywords" content="">
<meta name="description" content="">
<link rel="shortcut icon" href="favicon.ico">
<link href="ByFrame/css/bootstrap.min.css?v=3.3.6" rel="stylesheet">
<link href="ByFrame/css/font-awesome.css?v=4.4.0" rel="stylesheet">
<link href="ByFrame/css/plugins/bootstrap-table/bootstrap-table.min.css"
rel="stylesheet">
<link href="ByFrame/css/animate.css" rel="stylesheet">
<link href="ByFrame/css/style.css?v=4.1.0" rel="stylesheet">
<!-- 插件css -->
<link href="ByFrame/css/plugins/iCheck/custom.css" rel="stylesheet">
</head>
<!-- 全局js -->
<script src="ByFrame/js/jquery.min.js?v=2.1.4"></script>
<script src="ByFrame/js/bootstrap.min.js?v=3.3.6"></script>
<script src="ByFrame/js/plugins/metisMenu/jquery.metisMenu.js"></script>
<script src="ByFrame/js/plugins/slimscroll/jquery.slimscroll.min.js"></script>
<script src="ByFrame/js/plugins/layer/layer.min.js"></script>
<!-- 第三方插件 -->
<script src="ByFrame/js/plugins/pace/pace.min.js"></script>
<!-- iCheck -->
<script src="ByFrame/js/plugins/iCheck/icheck.min.js"></script>
<!-- Bootstrap table -->
<script src="ByFrame/js/plugins/bootstrap-table/bootstrap-table.min.js"></script>
<script
src="ByFrame/js/plugins/bootstrap-table/bootstrap-table-mobile.min.js"></script>
<script
src="ByFrame/js/plugins/bootstrap-table/locale/bootstrap-table-zh-CN.min.js"></script>
<style>
.col-center-block {
float: none;
display: block;
margin-left: auto;
margin-right: auto;
} .file {
position: relative;
/* display: inline-block; */
background: #D0EEFF;
border: 1px solid #99D3F5;
border-radius: 4px;
padding: 4px 12px;
overflow: hidden;
color: #1E88C7;
text-decoration: none;
text-indent: 0;
line-height: 20px;
}
.file input {
position: absolute;
font-size: 100px;
right: 0;
top: 0;
opacity: 0;
}
.file:hover {
background: #AADFFD;
border-color: #78C3F3;
color: #004974;
text-decoration: none;
}
</style>
<body class="gray-bg">
<div class="col-sm">
<!-- modal1-->
<div class="modal inmodal" id="modal1" tabindex="-1" role="dialog"
aria-hidden="true">
<div class="modal-dialog" style="width: 400px">
<div class="modal-content animated fadeIn">
<div class="modal-header" style="padding: 10px 8px 8px 0px">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span><span class="sr-only">Close</span>
</button>
<h6 id="yhTitle" class="modal-title">添加广告</h6>
</div>
<div class="modal-body"
style="width: 100%; padding: 5px 0px 5px 0px">
<div class="form-group row" style="text-align: center;">
<label class="col-xs-1 col-sm-3 control-label">广告标题:</label>
<div class="col-sm-8 ">
<input id="title" type="text" class="form-control">
</div>
</div>
<div class="form-group row" style="text-align: center;">
<!-- 上传按钮 -->
<div class="col-xs-1 col-sm-4" style="margin-left: 2%">
<a href="javascript:;" class="file">选择文件
<input type="file" name="file" id="upload-file">
</a>
</div>
<div class="col-sm-6" style="text-align:left;font-size: 1.9rem">
<span id="fileName"></span>
</div>
</div>
<div class="form-group row" style="text-align: center;">
<div class="col-sm-11 col-center-block" >
<!-- 进度条 -->
<div class="progress">
<div id="progress" data-picUrl="" class="progress-bar "
role="progressbar" aria-valuenow="60" aria-valuemin="0"
aria-valuemax="100" style="width: 0%;">
<span class="sr-only" style="position: static;"></span>
</div>
</div>
</div>
</div> </div>
<div class="modal-footer"
style="lenght: 10%; padding: 5px 0px 5px 0px">
<div class="text-center">
<button type="button" class="btn btn-primary" onclick="uploadFile();">开始上传</button>
<button type="button" class="btn btn-primary" onclick="addAdvertisement();">保存</button>
<button type="button" class="btn btn-primary"
data-dismiss="modal" onclick="cancleUploadFile();">取消</button>
</div>
</div> </div>
</div>
</div>
<!-- modal1-->
<div class="ibox float-e-margins">
<div class="ibox-title">
<div class="ibox-content">
<div class="row row-lg">
<div class="col-sm">
<div class="example">
<!-- Example Pagination -->
<div class="example-wrap">
<div class="example">
<table id="advertisementTable"> </table>
<!-- 工具容器 -->
<div id="toolbar" class="btn-group">
<button id="btn_add" type="button" class="btn btn-default"
onclick="addAdvertisementShow();">
<span class="glyphicon glyphicon-plus" aria-hidden="true"></span>新增
</button>
<button id="btn_edit" type="button" class="btn btn-default"
onclick="">
<span class="glyphicon glyphicon-pencil" aria-hidden="true"></span>修改
</button>
<button id="btn_delete" type="button" class="btn btn-default">
<span class="glyphicon glyphicon-remove" aria-hidden="true"></span>删除
</button>
</div> </div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</body>
<script type="text/javascript" src="js/advertisement.js"></script>
<script>
var xhr ;
function uploadFile(){
var fileObj = document.getElementById("upload-file").files[0]; // 获取文件对象
var FileController = "./advertisement/uploadPic"; // 接收上传文件的后台地址 if(fileObj){
console.log(fileObj);
// FormData 对象
var form = new FormData();
form.append("file", fileObj);// 文件对象
// XMLHttpRequest 对象
xhr = new XMLHttpRequest();
xhr.open("post", FileController, true);
xhr.onload = function () {
console.log(xhr.responseText);
var d = eval("(" +xhr.responseText+ ")"); //把数据转成json
//d.fileVal 后台返回的存储路径
//存储到进度条(data-picUrl)自定的属性里
$("#progress").attr("data-picUrl",d.fileVal)
};
xhr.onerror = function(){
console.log("上传失败")
}; //请求失败
xhr.upload.onprogress = function(evt){
if (evt.lengthComputable) {//
var progress = Math.round(evt.loaded / evt.total * 100) + "%";
$("#progress").css("width",progress);
$("#progress span").text(progress);
}
};
xhr.upload.onloadstart = function(){//上传开始执行方法
$("#fileName").text(fileObj.name);
};
xhr.send(form);
}else{
alert("未选择文件");
}
}
//取消文件上传
function cancleUploadFile(){
$("#fileName").text("");
$("#progress").css("width","0%");
$("#progress span").text("0%");
xhr.abort();
}
$(document).ready(function() {
//查询
initAdvertisement();
});
</script>
</html>
纯js上传文件 很好用的更多相关文章
- fromdata上传文件,ajax上传文件, 纯js上传文件,html5文件异步上传
前端代码: 上传附件(如支付凭证等) <input type="file" name="fileUpload" id="fileUpload&q ...
- 原生js上传文件,使用new FormData()
当创建一个内容较多的表单,表单里面又有了文件上传,文件上传也需要表单提交,单一的上传文件很好操作: <form action="接口" enctype="multi ...
- 使用ajaxfileupload.js上传文件
一直以来上传文件都是使用form表单上传文件,也看到过有人使用js上传文件,不过看起来蛮简单的也就没有怎么去理会.今天突然要使用这种方式上传文件,期间还遇到点问题.因此就记录下来,方便以后遇到这样的问 ...
- js 上传文件后缀名的判断 var flag=false;应用
js 上传文件后缀名的判断 var flag=false;应用 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional// ...
- js上传文件带参数,并且,返回给前台文件路径,解析上传的xml文件,存储到数据库中
ajaxfileupload.js jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId ...
- atitit.javascript js 上传文件的本地预览
atitit.javascript js 上传文件的本地预览 1. .URL.createObjectURL 1 1.1. 吊销所有使用 URL.createObjectURL 而创建的 URL,以 ...
- 前端js上传文件 到后端接收文件
下面是前端js代码: <html> <head> <meta http-equiv="Content-Type" content="text ...
- 纯js上传控件——fineuploader
fineuploader是一款基于ajax实现文件上传的插件,具有以下有点: A:支持文件上传进度显示. B:文件拖拽浏览器上传方式 C:Ajax页面无刷新. D:多文件上传. F:跨浏览器. E:跨 ...
- node.js 上传文件
在工作中碰到了这样的需求,需要用nodejs 来上传文件,之前也只是知道怎么通过浏览器来上传文件, 用nodejs的话, 相当于模拟浏览器的行为. google 了一番之后, 明白了浏览器无非就 ...
随机推荐
- Spring-data-redis: serializer实例
spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷.sdr提供了4种内置的serializer: JdkSerializationRe ...
- SpringMVC + MyBatis + Mysql + Redis(作为二级缓存) 配置
2016年03月03日 10:37:47 标签: mysql / redis / mybatis / spring mvc / spring 33805 项目环境: 在SpringMVC + MyBa ...
- HDU 1573 X问题(中国剩余定理标准解法)
X问题 Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- LOCK TABLES 和 UNLOCK TABLES
MySQLdump的时LOCK TABLES 和 UNLOCK TABLES 在mysqldump后的数据中会发现有 LOCK TABLES tables_name WRITE;和结尾处有 UNLOC ...
- jdk1.8学习、jdk1.9学习、jdk10.0学习和总结
由于中文参考资料很少,参考链接: https://www.oschina.net/translate/109-new-features-in-jdk-10 http://chuansong.me/n/ ...
- golang ffmpeg 做网络直播
最近在公司做在线视频转码的工作,研究了下ffmpeg 最后直接研究了下网络直播,我是在我自己的mac 上面测试的,效果,还可以,先看看效果图吧 ffmpeg 我是通过brew安装 的,这步就略了 VL ...
- codeforces 1037
题解: E-trips 哎哎哎好傻逼啊 没有想到算不能的一直在想怎么算能的 太傻逼了 其实很简单 我们只需要对好友<=k的首先dfs一下给他连接着的朋友-1 然后如果小于了就递归下去 这个正确性 ...
- mysql时间延时注入案例
通过Burpsuite结合sqlmap发现如下接口存在时间盲注
- Consumer is not subscribed to any topics or assigned any partitions
版本: scala:2.11.8 spark:2.11 hbase:1.2.0-cdh5.14.0 报错信息: java.lang.IllegalStateException: Consumer is ...
- Linux 记录所有用户登录和操作的详细日志
1.起因 最近Linux服务器上一些文件呗篡改,想追查已经查不到记录了,所以得想个办法记录下所有用户的操作记录. 一般大家通常会采用history来记录,但是history有个缺陷就是默认是1000行 ...