1.代码基础知识
(1).自执行函数格式如下
(function(){ /* code */ }());
!function(){ /* code */ }();
~function(){ /* code */ }();
-function(){ /* code */ }();
+function(){ /* code */ }();
(2).jquery扩展方案
$.extend(a,b) 将我们实现的b对象以及方法,加入a
2.实现类型
img,video
使用:移动端H5
3.代码如下
(function($) {
$.extend($.fn, {//0.扩展方法
fileUpload: function(opts) {
//1.遍历 //this==$("img") 和下面的调用方法相结合,如果是其他对象就是其他对象
this.each(function() {
//1.1使用$jquery选取方法
var $self = $(this);
//1.2页面元素声明doms元素 页面上的上传按钮,缩略图,进度条,文件类型;然后进行元素绑定
var doms = {
"fileToUpload": $self.find(".fileToUpload"),
"thumb": $self.find(".thumb"),
"video": $self.find(".video"),
"progress": $self.find(".upload-progress"),
};
//1.3页面元素声明主要方法
var funs = {
//选择文件,获取文件大小,也可以在这里获取文件格式,限制用户上传非要求格式的文件
//1.31 内部方法:文件选择,使用上述Dom元素
"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();
},
//1.32异步上传文件(表单制)
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];
var type=file.type;
fd.append(opts.file, file);//将文件添加到表单数据中
if ("image"==(funs.compareType(type))) {
funs.previewImage(file);//上传前预览图片,也可以通过其他方法预览txt
} else if("video"==(funs.compareType(type))){
funs.previewVideo(file);//上传前预览视频,也可以通过其他方法预览txt
}
}
//1.321 传统的XmlRequest方法,此方式一定会有跨域的问题
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);
},
//类型比较方法
compareType:function(type){
var imgType = /^image\//;
var videoType = /^video\//;
if(imgType.test(type)){
return "image";
}
if(videoType.test(type)){
return "video";
}
},
//视频预览
previewVideo: function(file) {
var gallery = doms.video;
var video = document.createElement("video");
video.setAttribute('controls','controls');
//video.style.width = "100%";
video.file = file;
doms.video.html(video);
//doms.style.display = "block";
// 使用FileReader方法显示图片内容
var reader = new FileReader();
reader.onload = (function(aVideo) {
return function(e) {
aVideo.src = e.target.result;
};
})(video);
reader.readAsDataURL(file);
video.pause();
},
//文件预览
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) {
alert(evt.target.responseText)
}
};
doms.fileToUpload.on("change", function() {
doms.progress.find("span").width("0");
funs.fileSelected();
});
});
}
});
})(jQuery);//Zepto
4.使用
<div class="camera-area">
<form enctype="multipart/form-data" method="post">
<input type="file" name="fileToUpload" class="fileToUpload" accept="image/*" capture="camera"/>
<div class="upload-progress"><span></span></div>
</form>
<div class="thumb"></div>
</div>
<div class="cam">
<form enctype="multipart/form-data" method="post">
<input type="file" name="fileToUpload" class="fileToUpload" accept="image/*" capture="camera"/>
<div class="upload-progress"><span></span></div>
</form>
<div class="video"></div>
</div>
<script>
$(".camera-area").fileUpload({
"url": "upload",
"file": "myFile"
});
$(".cam").fileUpload({
"url": "upload",
"file": "myFile"
});
</script>