jQuery插件ASP.NET应用之AjaxUpload
本次使用AJAXUPLOAD做为上传客户端无刷上传插件,其最新版本为3.9,官方地址:http://valums.com/ajax-upload/
在页面中引入 jquery.min.1.4.2.js 和 ajaxupload.js
- <script src="Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
- <script src="Scripts/ajaxupload3.9.js" type="text/javascript"></script>
HTML 代码:
- <style type="text/css">
- #txtFileName {
- width: 300px;
- }
- .btnsubmit{border-bottom: #cc4f00 1px solid; border-left: #ff9000 1px solid;border-top: #ff9000 1px solid; border-right: #cc4f00 1px solid;text-align: center; padding: 2px 10px; line-height: 16px; background: #e36b0f; height: 24px; color: #fff; font-size: 12px; cursor: pointer;}
- </style>
- 上传图片:<input type="text" id="txtFileName" /><div id="btnUp" style="width:300px;" class=btnsubmit >浏览</div>
- <div id="imglist"></div>
js代 码:
- <script type="text/javascript">
- $(function () {
- var button = $('#btnUp'), interval;
- new AjaxUpload(button, {
- //action: 'upload-test.php',文件上传服务器端执行的地址
- action: '/handler/AjaxuploadHandler.ashx',
- name: 'myfile',
- onSubmit: function (file, ext) {
- if (!(ext && /^(jpg|jpeg|JPG|JPEG)$/.test(ext))) {
- alert('图片格式不正确,请选择 jpg 格式的文件!', '系统提示');
- return false;
- }
- // change button text, when user selects file
- button.text('上传中');
- // If you want to allow uploading only 1 file at time,
- // you can disable upload button
- this.disable();
- // Uploding -> Uploading. -> Uploading...
- interval = window.setInterval(function () {
- var text = button.text();
- if (text.length < 10) {
- button.text(text + '|');
- } else {
- button.text('上传中');
- }
- }, 200);
- },
- onComplete: function (file, response) {
- //file 本地文件名称,response 服务器端传回的信息
- button.text('上传图片(只允许上传JPG格式的图片,大小不得大于150K)');
- window.clearInterval(interval);
- // enable upload button
- this.enable();
- var k = response.replace("<PRE>", "").replace("</PRE>", "");
- if (k == '-1') {
- alert('您上传的文件太大啦!请不要超过150K');
- }
- else {
- alert("服务器端传回的串:"+k);
- alert("本地文件名称:"+file);
- $("#txtFileName").val(k);
- $("<img />").appendTo($('#imglist')).attr("src", k);
- }
- }
- });
- });
- </script>
服务器端 ajaxuploadhandler.ashx 代码
- public void ProcessRequest(HttpContext context)
- {
- context.Response.ContentType = "text/plain";
- HttpPostedFile postedFile = context.Request.Files[0];
- string savePath = "/upload/images/";
- int filelength = postedFile.ContentLength;
- int fileSize = 163600; //150K
- string fileName = "-1"; //返回的上传后的文件名
- if (filelength <= fileSize)
- {
- byte[] buffer = new byte[filelength];
- postedFile.InputStream.Read(buffer, 0, filelength);
- fileName = UploadImage(buffer, savePath, "jpg");
- }
- context.Response.Write(fileName);
- }
- public static string UploadImage(byte[] imgBuffer, string uploadpath, string ext)
- {
- try
- {
- System.IO.MemoryStream m = new MemoryStream(imgBuffer);
- if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath)))
- Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath));
- string imgname = CreateIDCode() + "." + ext;
- string _path = HttpContext.Current.Server.MapPath(uploadpath) + imgname;
- Image img = Image.FromStream(m);
- if (ext == "jpg")
- img.Save(_path, System.Drawing.Imaging.ImageFormat.Jpeg);
- else
- img.Save(_path, System.Drawing.Imaging.ImageFormat.Gif);
- m.Close();
- return uploadpath + imgname;
- }
- catch (Exception ex)
- {
- return ex.Message;
- }
- }
- public static string CreateIDCode()
- {
- DateTime Time1 = DateTime.Now.ToUniversalTime();
- DateTime Time2 = Convert.ToDateTime("1970-01-01");
- TimeSpan span = Time1 - Time2; //span就是两个日期之间的差额
- string t = span.TotalMilliseconds.ToString("0");
- return t;
- }
jQuery插件ASP.NET应用之AjaxUpload的更多相关文章
- ASP.NET中使用jQuery插件实现图片幻灯效果
参照网上的资料及提供的jQuery插件实现图片幻灯效果. 1.页面前台代码: //头部引用 <head runat="server"><title>< ...
- ASP.NET- 无刷新上传使用jQuery插件之ajaxFileUpload
灰常好,我已经使用过里面的代码了,可以用,原文地址:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 一.ajaxFil ...
- JQuery插件定义
一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...
- jQuery插件入门
一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写("#"),("#"),("."),写了几年就对别人说非常熟悉JQ ...
- jPList – 实现灵活排序和分页功能的 jQuery 插件
jPList 是一个灵活的 jQuery 插件,可以用于任何 HTML 结构的排序,分页和筛选.它支持的数据源包括:PHP + MySQL,ASP.NET + SQL Server,PHP + SQL ...
- 不定义JQuery插件,不要说会JQuery 分类: JavaScript 2014-11-24 14:18 155人阅读 评论(0) 收藏
一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...
- jQuery 插件autocomplete
jQuery 插件autocomplete 自动加载 参考: http://www.cnblogs.com/Peter-Zhang/archive/2011/10/22/2221147.html ht ...
- [转]不定义JQuery插件,不要说会JQuery
一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写("#"),("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人,直 ...
- 解决jQuery插件冲突
项目框架的JS库集成了jQuery,Layout页面(模板页面,类似ASP.NET的母版页)中引用了这些JS,后来使用图表插件(图表插件是基于jQuery的)的时候,项目框架中的JS和图表插件有冲突, ...
随机推荐
- HDU 4747 Mex 递推/线段树
题目链接: acm.hdu.edu.cn/showproblem.php?pid=4747 Mex Time Limit: 15000/5000 MS (Java/Others)Memory Limi ...
- fastjson&gson
1.model转fastjson时,model成员变量是对象的,再转成fastjson时,不能仅仅判断key是否存在.应该判断其值是否为"". 2.gson 在 dao层貌似没有用 ...
- ACM-ICPC 2018 沈阳赛区网络预赛
Supreme Number 1000ms 131072K A prime number (or a prime) is a natural number greater than 111 tha ...
- 初识 es6之 const
const声明一个只读的常量.一旦声明,常量的值就不能改变. 例子: const a=12; a=2;//报错,const 声明的是常量,不能改 const声明的变量不得改变值,这意味着,const一 ...
- idea 复制数据库查询结果,粘贴直接生成insert into 语句
遇到一个场景,需要将数据库查询的结果导入到另外一个数据库中,给我的第一感受是,写程序,从数据库A中获取到数据,在插入到数据库B中. 但是Idea 可以直接复制查询结果,然后粘贴生成insert语句. ...
- Android性能测试工具:Emmagee介绍
简介 Emmagee是监控指定被测应用在使用过程中占用机器的CPU.内存.流量资源的性能测试小工具.该工具的优势在于如同windows系统性能监视器类似,它提供的是数据采集的功能,而行为则基于用户真实 ...
- CPP 替代 PIL 图片处理(缩略图生成)
python中使用PIL(Pyhton Image Library)进行图片处理,好处就是编写简单方便,但是不能很好利用机器多核的特点,于是在项目中决定使用cpp来实现图片处理. 项目中的图片处理主要 ...
- Halum UVA - 11478(差分约束 + 二分最小值最大化)
题意: 给定一个有向图,每条边都有一个权值,每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的边的权值增加d,最后要让所有边权的最小值非负且尽量大 两个特判 1 ...
- jQuery多重事件绑定
1. <a> a标签默认绑定了一个onclick事件,当自己再写一个onclick事件的时候,默认自己写的那个优先执行. 如下程序,先执行(123),然后再发生跳转. <!DOCTY ...
- JavaWeb文件上传和下载
文件上传和下载在web应用中非常普遍,要在jsp环境中实现文件上传功能是非常容易的,因为网上有许多用java开发的文件上传组件,本文以commons-fileupload组件为例,为jsp应用添加文件 ...