纯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 了一番之后, 明白了浏览器无非就 ...
随机推荐
- RabbitMQ在java中基础使用
RabbitMQ相关术语: 1.Broker:简单来说就是消息队列服务器实体. 2.Exchange:消息交换机,它指定消息按什么规则,路由到哪个队列. ...
- 查看CPU 内存 硬盘 网络 查看进程使用的文件 uptime top ps -aux vmstat iostat iotop nload iptraf nethogs
#安装命令 yum install sysstat #包含 iostat vmstat yum install iotop yum install nload yum install iptraf ...
- lsattr chattr 文件安全设置
[root@test_android_client_download xianlaigames]# lsattr -------------e- ./jilinmj.apk[root@test_and ...
- SQL Server索引维护
索引维护的两个重要方面是索引碎片和统计信息. 一:索引碎片 降低碎片的产生,当索引上的页不在具有物理连续性时,就会产生碎片,下面的情景会产生碎片: INSERT操作.UPDATE操作.DBCC SHR ...
- Nginx中配置undertow进行两个项目的动静分离配置
#user nobody; worker_processes ; pid /var/run/nginx.pid; events { worker_connections ; } http { incl ...
- 解决AS gradle下载同步卡慢的问题
国内因为GFW的原因,导致同步谷歌等服务器的插件源非常非常慢,几乎是龟爬,还好有阿里云的镜像源,据说速度很快,还不快试试: 1.build.gradle里的buildscript和allproject ...
- 关于 java.lang.ClassCastException: java.math.BigDecimal cannot be cast to java.lang.String
今天遇到了这个异常,其实是自己经验欠缺所致.我是通过mybatis查询到数据库传过来的主键,是一个32位的char类型. 代码: //查询总账表的该组织总账记录,包括该条记录的主键id.账户余额Dzz ...
- Go语言之匿名函数
匿名函数和字面量函数一样, 凡是可以使用字面量函数的地方,都可以用匿名函数代替. 这个和js中的匿名函数差不多吧. package main import "fmt" var su ...
- nssm部署.net core console到windows服务
轻便式发布 与.net fx不同 需要dotnet.exe来引导 Path:默认为 C:\Program Files\dotnet\dotnet.exe: Startup directory:程序所在 ...
- P1892 [BOI2003]团伙 并查集
题目描述 1920年的芝加哥,出现了一群强盗.如果两个强盗遇上了,那么他们要么是朋友,要么是敌人.而且有一点是肯定的,就是: 我朋友的朋友是我的朋友: 我敌人的敌人也是我的朋友. 两个强盗是同一团伙的 ...