本次使用AJAXUPLOAD做为上传客户端无刷上传插件,其最新版本为3.9,官方地址:http://valums.com/ajax-upload/

在页面中引入 jquery.min.1.4.2.js 和 ajaxupload.js

  1. <script src="Scripts/jquery-1.4.2.min.js" type="text/javascript"></script>
  2. <script src="Scripts/ajaxupload3.9.js" type="text/javascript"></script>

HTML 代码:

  1. <style type="text/css">
  2. #txtFileName {
  3. width: 300px;
  4. }
  5. .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;}
  6. </style>
  7. 上传图片:<input type="text" id="txtFileName" /><div  id="btnUp" style="width:300px;" class=btnsubmit >浏览</div>
  8. <div id="imglist"></div>

js代 码:

  1. <script type="text/javascript">
  2. $(function () {
  3. var button = $('#btnUp'), interval;
  4. new AjaxUpload(button, {
  5. //action: 'upload-test.php',文件上传服务器端执行的地址
  6. action: '/handler/AjaxuploadHandler.ashx',
  7. name: 'myfile',
  8. onSubmit: function (file, ext) {
  9. if (!(ext && /^(jpg|jpeg|JPG|JPEG)$/.test(ext))) {
  10. alert('图片格式不正确,请选择 jpg 格式的文件!', '系统提示');
  11. return false;
  12. }
  13. // change button text, when user selects file
  14. button.text('上传中');
  15. // If you want to allow uploading only 1 file at time,
  16. // you can disable upload button
  17. this.disable();
  18. // Uploding -> Uploading. -> Uploading...
  19. interval = window.setInterval(function () {
  20. var text = button.text();
  21. if (text.length < 10) {
  22. button.text(text + '|');
  23. } else {
  24. button.text('上传中');
  25. }
  26. }, 200);
  27. },
  28. onComplete: function (file, response) {
  29. //file 本地文件名称,response 服务器端传回的信息
  30. button.text('上传图片(只允许上传JPG格式的图片,大小不得大于150K)');
  31. window.clearInterval(interval);
  32. // enable upload button
  33. this.enable();
  34. var k = response.replace("<PRE>", "").replace("</PRE>", "");
  35. if (k == '-1') {
  36. alert('您上传的文件太大啦!请不要超过150K');
  37. }
  38. else {
  39. alert("服务器端传回的串:"+k);
  40. alert("本地文件名称:"+file);
  41. $("#txtFileName").val(k);
  42. $("<img />").appendTo($('#imglist')).attr("src", k);
  43. }
  44. }
  45. });
  46. });
  47. </script>

服务器端 ajaxuploadhandler.ashx 代码

  1. public void ProcessRequest(HttpContext context)
  2. {
  3. context.Response.ContentType = "text/plain";
  4. HttpPostedFile postedFile = context.Request.Files[0];
  5. string savePath = "/upload/images/";
  6. int filelength = postedFile.ContentLength;
  7. int fileSize = 163600; //150K
  8. string fileName = "-1"; //返回的上传后的文件名
  9. if (filelength <= fileSize)
  10. {
  11. byte[] buffer = new byte[filelength];
  12. postedFile.InputStream.Read(buffer, 0, filelength);
  13. fileName = UploadImage(buffer, savePath, "jpg");
  14. }
  15. context.Response.Write(fileName);
  16. }
  17. public static string UploadImage(byte[] imgBuffer, string uploadpath, string ext)
  18. {
  19. try
  20. {
  21. System.IO.MemoryStream m = new MemoryStream(imgBuffer);
  22. if (!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath)))
  23. Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath));
  24. string imgname = CreateIDCode() + "." + ext;
  25. string _path = HttpContext.Current.Server.MapPath(uploadpath) + imgname;
  26. Image img = Image.FromStream(m);
  27. if (ext == "jpg")
  28. img.Save(_path, System.Drawing.Imaging.ImageFormat.Jpeg);
  29. else
  30. img.Save(_path, System.Drawing.Imaging.ImageFormat.Gif);
  31. m.Close();
  32. return uploadpath + imgname;
  33. }
  34. catch (Exception ex)
  35. {
  36. return ex.Message;
  37. }
  38. }
  39. public static string CreateIDCode()
  40. {
  41. DateTime Time1 = DateTime.Now.ToUniversalTime();
  42. DateTime Time2 = Convert.ToDateTime("1970-01-01");
  43. TimeSpan span = Time1 - Time2;   //span就是两个日期之间的差额
  44. string t = span.TotalMilliseconds.ToString("0");
  45. return t;
  46. }

jQuery插件ASP.NET应用之AjaxUpload的更多相关文章

  1. ASP.NET中使用jQuery插件实现图片幻灯效果

    参照网上的资料及提供的jQuery插件实现图片幻灯效果. 1.页面前台代码: //头部引用 <head runat="server"><title>< ...

  2. ASP.NET- 无刷新上传使用jQuery插件之ajaxFileUpload

    灰常好,我已经使用过里面的代码了,可以用,原文地址:http://www.cnblogs.com/kissdodog/archive/2012/12/15/2819025.html 一.ajaxFil ...

  3. JQuery插件定义

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...

  4. jQuery插件入门

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写("#"),("#"),("."),写了几年就对别人说非常熟悉JQ ...

  5. jPList – 实现灵活排序和分页功能的 jQuery 插件

    jPList 是一个灵活的 jQuery 插件,可以用于任何 HTML 结构的排序,分页和筛选.它支持的数据源包括:PHP + MySQL,ASP.NET + SQL Server,PHP + SQL ...

  6. 不定义JQuery插件,不要说会JQuery 分类: JavaScript 2014-11-24 14:18 155人阅读 评论(0) 收藏

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写$("#"),$("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人 ...

  7. jQuery 插件autocomplete

    jQuery 插件autocomplete 自动加载 参考: http://www.cnblogs.com/Peter-Zhang/archive/2011/10/22/2221147.html ht ...

  8. [转]不定义JQuery插件,不要说会JQuery

    一:导言 有些WEB开发者,会引用一个JQuery类库,然后在网页上写一写("#"),("."),写了几年就对别人说非常熟悉JQuery.我曾经也是这样的人,直 ...

  9. 解决jQuery插件冲突

    项目框架的JS库集成了jQuery,Layout页面(模板页面,类似ASP.NET的母版页)中引用了这些JS,后来使用图表插件(图表插件是基于jQuery的)的时候,项目框架中的JS和图表插件有冲突, ...

随机推荐

  1. 软工网络15个人作业4--alpha阶段个人总结

    一.个人总结 自我评价表 类别 具体技能和面试问题 现在的回答 毕业找工作 语言 最拿手的语言之一,代码量是多少 java,代码量大概两三千行吧 语言 最拿手的语言之二,代码量是多少 python,代 ...

  2. 【leetcode】215. Kth Largest Element in an Array

    Find the kth largest element in an unsorted array. Note that it is the kth largest element in the so ...

  3. url传带有汉字的参数乱码解决

    url传带有汉字的参数乱码解决 var reg = new RegExp("(^|&)createName=([^&]*)(&|$)"); var r = ...

  4. windows(32位 64位)下python安装mysqldb模块

    windows(32位 64位)下python安装mysqldb模块 www.111cn.net 编辑:mengchu9 来源:转载 本文章来给各位使用在此windows系统中的python来安装一个 ...

  5. Expect the Expected UVA - 11427(概率dp)

    题意: 每天晚上你都玩纸牌,如果第一次就赢了,就高高兴兴的去睡觉,如果输了就继续玩.假如每盘游戏你获胜的概率都为p,每盘游戏输赢独立.如果当晚你获胜的局数的比例严格大于p时才停止,而且每天晚上最多只能 ...

  6. [Violet]蒲公英 分块

    发现写算法专题老是写不动,,,, 所以就先把我在luogu上的题解搬过来吧! 题目大意:查询区间众数,无修改,强制在线 乍一看是一道恐怖的题,仔细一看发现并没有那么难: 大致思路是这样的,首先我们要充 ...

  7. 【BZOJ2879】【NOI2012】美食节(费用流)

    [BZOJ2879][NOI2012]美食节(费用流) 题面 BZOJ 洛谷 题解 一眼就会思路了吧. 把每个厨师拆点,拆分为他最多能要做的菜的个数,即\(\sum p_i\) 然后把每个菜向厨师的每 ...

  8. dorado事件

    //----------------------------------------------------------------------------// //校验实体数据是否填写if(enti ...

  9. UESTC--1727

    原题链接:http://acm.uestc.edu.cn/problem.php?pid=1727 分析:用 l[i] 记录第 i 层楼有多少物品需要往上继续搬运,如果某层楼没有物品,但是更上面还有, ...

  10. NYOJ--69

    数的长度 原题链接:http://acm.nyist.net/JudgeOnline/problem.php?pid=69 分析:先看看求n!的朴素算法,用大整数乘法来实现. #include< ...