使用Jquery书写实现上传文件的插件
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title></title>
<style type="text/css">
.input-file {
overflow:hidden;
position:relative;
}
.input-file input{
opacity:0;
filter:alpha(opacity=0);
font-size:100px;
position:absolute;
top:0;
right:0;
}
#uploadTable
{
width:500px;
border-collapse:collapse;
border:1px solid Silver;
}
</style>
<script src="Scripts/jquery-1.7.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
;(function ($) {
$.fn.SalesMOUNDUpload = function (options) {
var defaults =
{
saveUrl: '',
jqInput: '',
maxSize: 1024 * 1024 * 100, //100M
fnRemove: '', //移除文件 ,参数:文件名
fnComplete: '' //每个文件成功 ,参数:服务器端返回内容
};
var opt = $.extend(defaults, options);
function getByteToM(val) {
if (isNaN(val)) return val;
val = val / (1024 * 1024);
val = Math.round(val * 100) / 100;
return val;
}
return this.each(function () {
var $this = $(this);
$this.empty();
if (typeof FormData == 'undefine') { alert('浏览器版本太低,不支持改上传!'); return; }
//表头
if ($this.find('thead').length == 0) {
var $thead = $('<thead>')
var $th_tr = $('<tr>');
$th_tr.append('<th>文件名</th>');
$th_tr.append('<th>类型</th>');
$th_tr.append('<th>大小</th>');
$th_tr.append('<th>状态</th>');
$th_tr.append('<th>操作</th>');
$th_tr.appendTo($thead);
$this.append($thead);
}
opt.jqInput[0].addEventListener('change', function (e) {
var file = this.files[0];
if (!file) { return; }
if (file.size > opt.maxSize) {
alert('文件超过最大');
return;
}
var fd = new FormData();
var $table = $this;
fd.append("uploadFile", file);
var xhr = new XMLHttpRequest();
xhr.open('POST', opt.saveUrl, true);
xhr.upload.addEventListener("progress", uploadProgress, false);
xhr.addEventListener("load", uploadComplete, false);
xhr.addEventListener("error", uploadFailed, false);
xhr.addEventListener("abort", uploadCanceled, false);
//表中内容
var $tr = $('<tr>');
$tr.append('<td class="upload_name">' + file.name + '</td>');
$tr.append('<td class="upload_type">' + file.type + '</td>');
$tr.append('<td class="upload_size">' + getByteToM(file.size) + 'M' + '</td>');
$tr.append('<td class="upload_status">' + 0 + '</td>');
$tr.append('<td class="upload_action"><a href="javascript:void(0);">' + '取消' + '</a></td>');
$tr.find('.upload_action a').unbind('click').bind('click', function() {
xhr.abort();
});
$table.append($tr);
function uploadProgress(evt) {
if (evt.lengthComputable) {
var percentComplete = Math.round(evt.loaded * 100 / evt.total);
$tr.find('.upload_status').html(Math.round(percentComplete) + '%');
}
else {
$tr.find('.upload_status').html('unable to compute');
}
}
function uploadComplete(evt) {
if (evt.target.status == 200) {
$tr.find('.upload_status').html('已完成');
$tr.find('.upload_action a').html('删除');
if (typeof opt.fnComplete == 'function') {
opt.fnComplete(evt.target.response);
}
$tr.find('.upload_action').unbind('click').bind('click', removeFile);
}
}
function uploadFailed() {
$tr.find('.upload_status').html('<a href="javascript:void(0);">×</a>');
$tr.find('.upload_status a').unbind('click').bind('click', function () {
$tr.remove();
});
$tr.find('.upload_action a').html('重试');
$tr.find('.upload_action a').unbind('click').bind('click', function () {
xhr.send(fd);
});
}
function uploadCanceled() {
$tr.remove();
}
function removeFile() {
$tr.remove();
if (typeof opt.fnRemove == 'function') {
opt.fnRemove(file.name);
}
}
xhr.send(fd);
}, false);
});
};
} (jQuery));
</script>
<script type="text/javascript">
$(function () {
$('#uploadTable').SalesMOUNDUpload(
{
saveUrl: '/Test/Save',
jqInput: $('#txtFile'),
fnRemove: removeFile,
fnComplete: function (d) { console.log('complete ' + d); }
});
});
function removeFile(d) {
$.post('/Test/Remove', { "filename": d }, function (r) { });
}
</script>
</head>
<body>
<table id="uploadTable" border="1px"></table>
<a href="javascript:void(0);" class="input-file">
添加文件
<input type="file" id="txtFile" style="width:200px;" />
</a>
</body>
</html>
初心商城:初心商城
作者:韩迎龙(Kencery) MVC/.NET群:159227188如果您认为这篇文章还不错或者有所收获,您可以通过右边的“打赏”功能 打赏一杯咖啡,本页版权归作者和博客园所有,欢迎转载,但未经作者同意必须保留此段声明, 且在文章页面明显位置给出原文链接,否则保留追究法律责任的权利
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
2012-10-28 多线程学习笔记第三篇