纯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 了一番之后, 明白了浏览器无非就 ...
随机推荐
- social psychology 10th David G. Myers
Social psychology is a science that studies the influences of our situations, with special attention ...
- 20165328 学习基础和C语言基础调查
一.技能学习经验: 1.你有什么技能比大多数人(超过90%以上)更好: 我算是一个普通人,没什么特别的才能,如果硬要说有什么技能比其他人较好的话,我想大概是快速阅读的能力吧,我能以很快的速度 ...
- 【java细节】Java代码忽略https证书:No subject alternative names present
https://blog.csdn.net/audioo1/article/details/51746333
- 磁盘blk_update_request: I/O error
https://www.cnblogs.com/chris-cp/p/6340289.html
- webpack学习笔记--整体配置结构
之前的章节分别讲述了每个配置项的具体含义,但没有描述它们所处的位置和数据结构,下面通过一份代码来描述清楚: const path = require('path'); module.exports = ...
- .net remoting(1)简单例子
1.例子(程序间的通讯) class Program { static void Main(string[] args) { HttpChannel _channel = ); ChannelServ ...
- photoshop CC智能切图
网页设计在技术层面上,第一步是美工做出网页效果图,第二步就是网页前端进行网页切图.网页切图工具常用的有fireworks.PS,这里使用PS进行网页切图. 我们通过设计稿,得到我们想要的产出物(如.p ...
- 记JS一个错误,
在定义变量时候,如果直接定义 var obj=null;那么,在转码时候,比如转为base64,此时或出现特殊的文字(也是汉字,只是很少见少用的文字,而且,出现的文字是根据你给obj赋值决定的,) 由 ...
- 分布式系统的BASE理论
一.BASE理论 eBay的架构师Dan Pritchett源于对大规模分布式系统的实践总结,在ACM上发表文章提出BASE理论,BASE理论是对CAP理论的延伸,核心思想是即使无法做到强一致性(St ...
- Python_collections_OrderedDict有序字典部分功能介绍
OrderedDict():实现字典的固定排序,是字典的子类 import collections dic = collections.OrderedDict() dic['k1'] = 3 dic[ ...