自制文件上传JS控件,支持IE、chrome、firefox etc.
2014-04-17 22:33
561 查看
(function() { if (window.FileUpload) { return; } window.FileUpload = function (id, url) { this.id = id; this.autoUpload = true; this.url = url; this.maxSize = null; this.extensions = null; this.dropId = null; }; window.FileUpload.prototype.init = function() { var obj = this; $('#' + this.id).change(function () { if (obj.autoUpload) { obj.upload(); } }); if (this.supportsFormData()) { if (this.dropId != null) { var drop = $('#' + this.dropId)[0]; drop.addEventListener("dragover", function(e) { e.stopPropagation(); e.preventDefault(); $('#' + obj.dropId).addClass("dragover"); }, false); drop.addEventListener("dragout", function(e) { $('#' + obj.dropId).removeClass("dragover"); }, false); drop.addEventListener("drop", function(e) { e.stopPropagation(); e.preventDefault(); $('#' + obj.dropId).removeClass("dragover"); obj._uploadUsingFormData(e.dataTransfer.files[0]); }, false); } } else { if (this.dropId != null) { $('#' + this.dropId).hide(); } } }; FileUpload.prototype.supportsFormData = function() { return window.FormData != undefined; }; FileUpload.prototype.upload = function() { if (this.supportsFormData()) { this._uploadUsingFormData($("#" + this.id)[0].files[0]); } else { this._uploadUsingFrame(); } }; FileUpload.prototype._uploadUsingFrame = function() { var obj = this; var $frame = $('#uploadFrame'); if ($frame.length == 0) { $frame = $('<iframe id="uploadFrame" width="0" height="0" name="uploadFrame" src=""></iframe>'); $frame.appendTo("body"); $frame.load(function() { var response = $frame.contents().text(); try { var json = $.parseJSON(response); $(obj).trigger("onLoad", json); } catch(ex) { $(obj).trigger("onError", response); } }); } var form = $("#" + this.id).closest("form")[0]; form.target = 'uploadFrame'; form.submit(); }; FileUpload.prototype._uploadUsingFormData = function (file) { var obj = this; var xhr = new XMLHttpRequest(); xhr.addEventListener("load", function (e) { var json = $.parseJSON(xhr.response); $(obj).trigger("onLoad", json); }, false); xhr.addEventListener("error", function (e) { $(obj).trigger("onError", e); }, false); xhr.upload.addEventListener("progress", function (e) { if (e.lengthComputable) { $(obj).trigger("onProgress", e.loaded, e.total); } }, false); xhr.open("POST", obj.url); if (obj.maxSize != null&&file.size>obj.maxSize) { $(obj).trigger("onMaxSizeError"); return; } if (obj.extensions != null) { var name = file.name; var ext = name.substring(name.lastIndexOf("."), name.length).toLowerCase(); if (obj.extensions.indexOf(ext) < 0) { $(obj).trigger("onExtensionError"); return; } } var formData = new FormData(); formData.append("files", file); xhr.send(formData); }; FileUpload.prototype.onLoad = function(handler) { $(this).bind("onLoad", function(sender, args) { handler && handler(args); }); }; FileUpload.prototype.onProgress = function (handler) { $(this).bind("onProgress", function(sender, loaded, total) { handler && handler(loaded, total); }); }; FileUpload.prototype.onError = function (handler) { $(this).bind("onError", function(sender, error) { handler && handler(error); }); }; FileUpload.prototype.onMaxSizeError = function(handler) { $(this).bind("onMaxSizeError", handler); }; FileUpload.prototype.onExtensionError = function (handler) { $(this).bind("onExtensionError", handler); }; })();
<form enctype="multipart/form-data" id="ctl01" action="Default.aspx" method="post">
相关文章推荐
- js获取上传文件大小,兼容ie,FireFox,Chrome
- js实现上传文件大小限制,兼容IE、Firefox、Chrome
- js实现文件批量上传,支持ie firefox
- js判断上传文件的大小,及确认文件是否在本地存在,兼容firefox、chrome、ie等浏览器
- js限制本地上传文件大小(支持ie6,7,8 firefox 谷歌)
- JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox
- 基于项目需要,再次修正了js版的梅花雨日历控件,让它支持FireFox和Chrome
- js获取上传图片全路径,支持ie,firefox
- 关于 IE firefox Chrome下的通过用js 关闭窗口部分不支持问题解决
- JS 在页面上直接将json数据导出到excel,支持chrome,edge,IE10+,IE9,IE8,Safari,Firefox
- 支持firefox10和IE的上传预览图片js代码
- [完美]原生JS获取浏览器版本判断--支持Edge,IE,Chrome,Firefox,Opera,Safari,以及各种使用Chrome和IE混合内核的浏览器
- 支持firefox10和IE的上传预览图片js代码
- 文件上传(firefox 1.01 和ie 6.0中上传控件的区别)
- js实现上传图片实时预览,兼容chrome IE firefox多浏览器
- 在IE中必须点击input file 控件的浏览按钮才能提交上传,否则没有权限 。js文件报拒绝访问,解决办法是将input file控件定位到你要点击的地方 ,并且input file做成透明的
- js 获取上传图片 全路径,支持ie,firefox
- uploadify在ie,chrome等浏览器上传文件成功,而firefox出现http error 404问题的解决方法
- js兼容ie和chrome的文件上传大小校验