LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui

LoT.UI开源地址如下:https://github.com/dunitian/LoTCodeBase/tree/master/LoTUI

先看在LoT.UI里面的应用效果图:

懒人福利:http://www.cnblogs.com/dunitian/p/5535455.html(一句代码直接实现)

关键代码解析:(https://github.com/dunitian/LoTCodeBase/tree/master/NetCode/3.常用技能/02.uploader系列/01.Webuploader

JS部分:

<script type="text/javascript">
//1.uploader初始化。 auto-是否自动上传
var uploader = WebUploader.create({
server: '/Home/Upload',
swf: '/open/webuploader/Uploader.swf',
pick: '#lot-picker',
auto: true, //自动上传
//chunked: true, //断点续传-单个默认5M
duplicate: false, //文件去重
prepareNextFile: true, //提前准备下一个文件(可选,单文件不建议开)
//formData: { }, //自定义参数表,每次请求都会发送这些参数
paste: document.body, //启动剪贴板(可选)
dnd: $('#lot-uploader'), //启动拖拽(可选)
fileNumLimit: 5, //文件总数量
fileSingleSizeLimit: 10485760, //单个文件最大值(IIS默认<=4M,可自行设置:httpRuntime的maxRequestLength)
accept: {
title: 'Images',
extensions: 'gif,jpg,jpeg,bmp,png',
mimeTypes: 'image/*'
}
}); //2.文件添加时,添加样式和缩略图
uploader.on('fileQueued', function (file) {
var $li = $(
'<div id="' + file.id + '" class="file-item thumbnail">' +
'<img>' +
'<div class="info info-top">Szie:' + Math.floor(file.size / 1024) + 'kb' + '</div>' +
'<div class="info info-bot">' + file.name + '</div>' +
'</div>'
),
$img = $li.find('img');
$('#lot-filelist').append($li);
// 创建缩略图
uploader.makeThumb(file, function (error, src) {
if (error) {
$img.replaceWith('<span>不能预览</span>');
return;
}
$img.attr('src', src);
}, 100, 100);
////计算文件MD5(可加)
//uploader.md5File(file).then(function (val) {
// console.log('md5:',val,'-',file.name);
//});
}); //3.文件上传过程中创建进度条实时显示。
uploader.on('uploadProgress', function (file, percentage) {
var $li = $('#' + file.id),
$percent = $li.find('.progress span');
//避免重复创建
if (!$percent.length) {
$percent = $('<p class="progress"><span></span></p>').appendTo($li).find('span');
}
$percent.css('width', percentage * 100 + '%');
}); //4.文件上传成功,给item添加成功class, 用样式标记上传成功。
uploader.on('uploadSuccess', function (file, data) {
if (data.status) {
$('#' + file.id).addClass('upload-state-done');
} else {
showMsg(file, data.msg);
}
}); //5.失败系列的处理
//5.1添加失败处理
uploader.on('error', function (log) {
switch (log) {
case 'F_EXCEED_SIZE':
addMsg('文件10M以内'); break;
case 'Q_EXCEED_NUM_LIMIT':
addMsg('已超最大上传数'); break;
case 'Q_TYPE_DENIED':
addMsg('文件类型不正确'); break;
case 'F_DUPLICATE':
addMsg('文件已经被添加'); break;
default:
addMsg('文件添加失败~'); break;
}
});
//5.2上传失败处理
uploader.on('uploadError', function (file) {
showMsg(file, '上传失败');
}); //错误信息显示
function showMsg(file, msg) {
var $li = $('#' + file.id), $error = $li.find('div.error');
//避免重复创建
if (!$error.length) {
$error = $('<div class="error"></div>').appendTo($li);
}
$error.text(msg);
}
//错误信息提示
function addMsg(msg) {
$('#lot-uploader').prepend('<h3 class="temp-log" style="color:red;">' + msg + '</h3>')
setTimeout(function () {
$('.temp-log').remove();
}, 2000);
}
</script>

后端代码:

/// <summary>
/// 图片上传
/// </summary>
/// <returns></returns>
public JsonResult Upload(HttpPostedFileBase file)
{
if (file == null) { return Json(new { status = false, msg = "图片提交失败" }); }
if (file.ContentLength > 10485760) { return Json(new { status = false, msg = "文件10M以内" }); }
string filterStr = ".gif,.jpg,.jpeg,.bmp,.png";
string fileExt = Path.GetExtension(file.FileName).ToLower();
if (!filterStr.Contains(fileExt)) { return Json(new { status = false, msg = "图片格式不对" }); }
//防止黑客恶意绕过,从根本上判断下文件后缀
if (!file.InputStream.CheckingExt())
{
//todo:一次危险记录
return Json(new { status = false, msg = "图片格式不对" });
}
//todo: md5判断一下文件是否已经上传过,如果已经上传直接返回 return Json(new { status = true, msg = sqlPath }); string path = string.Format("{0}/{1}", "/lotFiles", DateTime.Now.ToString("yyyy-MM-dd"));
string fileName = string.Format("{0}{1}", Guid.NewGuid().ToString("N"), fileExt);
string sqlPath = string.Format("{0}/{1}", path, fileName);
string dirPath = Request.MapPath(path); if (!Directory.Exists(dirPath)) { Directory.CreateDirectory(dirPath); }
try
{
//todo:缩略图
file.SaveAs(Path.Combine(dirPath, fileName));
//todo: 未来写存数据库的Code
}
catch { return Json(new { status = false, msg = "图片保存失败" }); }
return Json(new { status = true, msg = sqlPath });
}

开源组件:https://github.com/fex-team/webuploader

扩展组件:https://github.com/dunitian/LoTUploader

06.LoT.UI 前后台通用框架分解系列之——浮夸的图片上传的更多相关文章

  1. 07.LoT.UI 前后台通用框架分解系列之——强大的文本编辑器

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  2. 07.LoT.UI 前后台通用框架分解系列之——轻巧的文本编辑器

    LoT.UI汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui 上次说的是强大的百度编辑器 http://www.cnblogs.com/d ...

  3. 08.LoT.UI 前后台通用框架分解系列之——多样的Tag选择器

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  4. 01.LoT.UI 前后台通用框架分解系列之——小图片背景全屏显示(可自动切换背景)

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  5. 02.LoT.UI 前后台通用框架分解系列之——灵活的菜单栏

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  6. 03.LoT.UI 前后台通用框架分解系列之——多样的表格

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  7. 04.LoT.UI 前后台通用框架分解系列之——轻巧的弹出框

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  8. 05.LoT.UI 前后台通用框架分解系列之——漂亮的时间选择器

    LOT.UI分解系列汇总:http://www.cnblogs.com/dunitian/p/4822808.html#lotui LoT.UI开源地址如下:https://github.com/du ...

  9. 网络请求框架----HttpClient的get,post和图片上传服务器

    HttpClient是Apache Jakarta Common下的子项目,用来提供高效的.最新的.功能丰富的支持HTTP协议的客户端编程工具包,并且它支持HTTP协议最新的版本和建议.HttpCli ...

随机推荐

  1. [BOT] 一种android中实现“圆角矩形”的方法

    内容简介 文章介绍ImageView(方法也可以应用到其它View)圆角矩形(包括圆形)的一种实现方式,四个角可以分别指定为圆角.思路是利用"Xfermode + Path"来进行 ...

  2. 探索C#之6.0语法糖剖析

    阅读目录: 自动属性默认初始化 自动只读属性默认初始化 表达式为主体的函数 表达式为主体的属性(赋值) 静态类导入 Null条件运算符 字符串格式化 索引初始化 异常过滤器when catch和fin ...

  3. java单向加密算法小结(1)--Base64算法

    从这一篇起整理一下常见的加密算法以及在java中使用的demo,首先从最简单的开始. 简单了解 Base64严格来说并不是一种加密算法,而是一种编码/解码的实现方式. 我们都知道,数据在计算机网络之间 ...

  4. Asp.net Core准备工作

    1.安装环境 安装.Net Core SDK 安装VS2015 Update3 安装DotNetCore.1.0.1-VS2015Tools.Preview2.0.2.exe 2.新建Core工程 项 ...

  5. 搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 (1)

    搭建QQ聊天通信的程序:(1)基于 networkcomms.net 创建一个WPF聊天客户端服务器应用程序 原文地址(英文):http://www.networkcomms.net/creating ...

  6. CSS三个定位——常规、浮动、绝对定位

    .dage { width: 868px; background: #5B8C75; border: 10px solid #A08C5A; margin-top: -125px; margin-le ...

  7. 热修复-Tinker

    微信开源,真是喜出望外,必须要去看看啊,比起nuwa来微信好很多,而且github上也有专门的官方文档说明,还有很多资料查询 参考地址:https://github.com/Tencent/tinke ...

  8. DockerCon 2016 – 微软带来了什么?

    根据Forrester的调查,接近半数的企业CIO在考虑IT架构的时候更乐于接受开源方案,这主要是基于低成本,避免供应商锁定和敏捷的需求:同时另外一家North Bridge的调研机构的调查显示,20 ...

  9. vim安装中文帮助手册

    安装方法:   在下面的网站下载中文帮助的文件包:$wget http://nchc.dl.sourceforge.net/sourceforge/vimcdoc/vimcdoc-1.5.0.tar. ...

  10. BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]

    1103: [POI2007]大都市meg Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2221  Solved: 1179[Submit][Sta ...