Html5上传插件封装
前段时间将flash的上传控件替换成使用纯js实现的,在此记录
1.创建标签
<div class="camera-area" style="display:inline-block;float:left">
<input type="file" id="UploadFile" name="fileToUpload" class="fileToUpload" style="float:left;width:auto;padding:10px 0" />
<div class="upload-progress"></div>
<div class="thumb">
<img id="myPhoto" />
</div>
</div>
div内部有3个标签 第一个是上传,第二个是上传进度,第三个为了上传的预览
2.封装上传插件
//拓展
$.extend($.fn, {
fileUpload: function (opts) {
this.each(function () {
var $self = $(this);
var doms = {
"fileToUpload": $self.find(".fileToUpload"),
"thumb": $self.find(".thumb"),
"progress": $self.find(".upload-progress")
};
var funs = {
//选择文件,获取文件大小,也可以在这里获取文件格式,限制用户上传非要求格式的文件
"fileSelected": function () {
var files = (doms.fileToUpload)[0].files;
var count = files.length;
for (var index = 0; index < count; index++) {
var file = files[index];
var fileSize = 0;
if (file.size > 1024 * 1024)
fileSize = (Math.round(file.size * 100 / (1024 * 1024)) / 100).toString() + 'MB';
else
fileSize = (Math.round(file.size * 100 / 1024) / 100).toString() + 'KB';
}
funs.uploadFile();
},
//异步上传文件
uploadFile: function () {
var fd = new FormData();//创建表单数据对象
var files = (doms.fileToUpload)[0].files;
var count = files.length;
for (var index = 0; index < count; index++) {
var file = files[index];
fd.append(opts.file, file);//将文件添加到表单数据中
funs.previewImage(file);//上传前预览图片,也可以通过其他方法预览txt
}
var xhr = new XMLHttpRequest();
xhr.upload.addEventListener("progress", funs.uploadProgress, false);//监听上传进度
xhr.addEventListener("load", funs.uploadComplete, false);
xhr.addEventListener("error", opts.uploadFailed, false); xhr.open("POST", opts.url);
xhr.send(fd);
},
//文件预览
previewImage: function (file) {
var gallery = doms.thumb;
var img = document.createElement("img");
img.file = file;
doms.thumb.html(img);
// 使用FileReader方法显示图片内容
var reader = new FileReader();
reader.onload = (function (aImg) {
return function (e) {
aImg.src = e.target.result;
};
})(img);
reader.readAsDataURL(file);
},
uploadProgress: function (evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
doms.progress.html(percentComplete.toString() + '%');
}
},
"uploadComplete": function (evt) {
var returnModel = JSON.parse(evt.target.responseText);
if (returnModel.url) {
$("#" + opts.id).val(returnModel.url);
}
if (!returnModel.success) {
alert(returnModel.msg);
$(".upload-progress").html("0%");
$(".thumb img").attr("src", "");
$("#" + opts.id).val("");
}
}
};
doms.fileToUpload.on("change", function () {
doms.progress.find("span").width("0");
funs.fileSelected();
});
});
}
});
3.调用封装的控件
$(".camera-area").fileUpload({
"url": "/Home/SavePhoto",
"file": "fileName",
"id": "Photo"
});
url:上传的位置
file:后台接收此文件的参数
id:当前是冗余拓展,博主本意是上传到服务器后返回个url,url指向上传文件的服务器路径
4.控制器接收文件并且保存(简单实现)
[HttpPost]
/// <summary>
/// 上传文件
/// </summary>
/// <returns></returns>
public ActionResult SavePhoto()
{
//fileName要和视图的插件参数一致
HttpPostedFileBase file = HttpContext.Request.Files["fileName"];
string savePath = Path.Combine(Server.MapPath("~/Temp/"), DateTime.Now.Year.ToString(), DateTime.Now.Month.ToString());
if (!Directory.Exists(savePath))
{
Directory.CreateDirectory(savePath);
}
string fileName = DateTime.Now.ToString("yyyyMMddHHmmss") + Path.GetExtension(file.FileName);
fileName = Path.Combine(savePath, fileName);
file.SaveAs(fileName);
return this.Json(new { success = true });
}
5.效果演示
Html5上传插件封装的更多相关文章
- zyUpload界面绝佳、体验超棒的HTML5上传插件
一.为毛线开发它 经过了两个星期做出了两个基于HTML5的多文件上传插件,之前在做网站的时候用到文件上传这一个功能,但是大多说都是基于Flash的,正好最近HTML5很火,而且渐渐壮大起来,感觉搞前端 ...
- 【ASP.NET 插件】zyUpload的HTML5上传插件
个人能力有限,只能网上找图片批量上传插件,看到一个还不错的插件zyUpload ,可以用来上传文件,但没有.NET 版本,特修改了下用以批量上传图片,效果图如下: update:2016年3月8日 有 ...
- 多文件上传插件Stream,是Uploadify的Flash版和Html5版的结合,带进度条,并支持html5断点续传(附件上传),拖拽等功能
是根据某网的文件上传插件加工而来,支持不同平台(Windows, Linux, Mac, Android, iOS)下,主流浏览器(IE7+, Chrome, Firefox, Safari, 其他) ...
- 基于vue-simple-uploader封装文件分片上传、秒传及断点续传的全局上传插件
目录 1. 前言 2. 关于vue-simple-uploader 3. 基于vue-simple-uploader封装全局上传组件 4. 文件上传流程概览 5. 文件分片 6. MD5的计算过程 7 ...
- 批量上传插件(flash,html5,jquery)
1.jQuery File Upload 官网:http://blueimp.github.com/jQuery-File-Upload/ 在线示例:http://blueimp.github.com ...
- 一款基于uploadify扩展的多文件上传插件,完全适用于Html5
http://www.uploadify.com/documentation/ 官网里面有两个插件,一个是要使用flash插件才能文件上传的插件,另外一个是不需要使用要flash插件的文件上传插件完 ...
- HTML5文件上传插件 Huploadify V2.1发布
月初发布了HUploadify2.0版本,增加了文件的断点续传功能,得到了不少朋友的好评.本着按照Uploadify原样复制的原则,本次在一些朋友的建议中采纳了几点,做了一次较小的改动,定为2.1版本 ...
- 强大的支持多文件上传的jQuery文件上传插件Uploadify
支持多文件上传的jQuery文件上传插件Uploadify,目前此插件有两种版本即Flash版本和HTML5版本,对于HTML5版本会比较好的支持手机浏览器,避免苹果手机Safari浏览器不支持Fla ...
- jQuery文件上传插件Uploadify(转)
一款基于flash的文件上传,有进度条和支持大文件上传,且可以多文件上传队列. 这款在flash的基础上增加了html5的支持,所以在移动端也可以使用. 由于官方提供的版本是flash免费,html5 ...
随机推荐
- BZOJ3531 [Sdoi2014]旅行 【树剖 + 线段树】
题目 S国有N个城市,编号从1到N.城市间用N-1条双向道路连接,满足 从一个城市出发可以到达其它所有城市.每个城市信仰不同的宗教,如飞天面条神教.隐形独角兽教.绝地教都是常见的信仰.为了方便,我们用 ...
- linux系统——hosts文件修改
1. 关于/etc/host,主机名和IP配置文件 Hosts - The static table lookup for host name(主机名查询静态表) Linux 的/etc/hosts是 ...
- Codeforces Round #362 (Div. 2) A 水也挂
A. Pineapple Incident time limit per test 1 second memory limit per test 256 megabytes input standar ...
- 【CF1043D】Mysterious Crime(贡献)
题意:给定m个人,每个人有n个数字且每个人的所有数字都是一个n的排列,求有多少种方案去掉某个前缀和后缀后m个人剩余的部分相等 m<=10,n<=1e5 思路:考虑极长的一段连续匹配的串,因 ...
- jquery取得iframe中元素的方法
原文发布时间为:2010-12-27 -- 来源于本人的百度文章 [由搬家工具导入] 收集利用Jquery取得iframe中元素的几种方法 :contents() $.trim($("if ...
- 牛客挑战赛14-F细胞
https://www.nowcoder.com/acm/contest/81/F 循环卷积的裸题,太久没做FFT了,这么裸的循环卷积都看不出来 注意一下本文的mod 都是指表示幂的模数,而不是NTT ...
- [LeetCode] Linked List Cycle II 链表环起始位置
Given a linked list, return the node where the cycle begins. If there is no cycle, return null. Foll ...
- 【eclipse】导入/导出开发环境(包括编辑器字体颜色大小等)
Eclipse的 File -> Export(导出),在窗口中展开 General(常规) -> Perferences(首选项)-->Export all(全部导出)然后点击 N ...
- win10下怎么配置以KDiff3作为merge tool和diff tool
系统环境: OS: Windows 10 Git 2.6.1.windows.1 KDiff3 0.9.98 (64 bit) 具体代码如下: git config --global --add me ...
- Manjaro linux软件源设置
1.从官方http://jaist.dl.sourceforge.net/project/manjarotest/16.06-dev/kde/minimal/manjaro-kde-minimal-1 ...