首页 > 网站 > WEB开发 > 正文

jQuery插件ASP.NET应用之AjaxUpload

2024-04-27 14:19:12
字体:
来源:转载
供稿:网友

jQuery插件asp.net应用之AjaxUpload

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

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

Html代码收藏代码
  1. <scriptsrc="Scripts/jquery-1.4.2.min.js"type="text/javascript"></script>
  2. <scriptsrc="Scripts/ajaxupload3.9.js"type="text/Javascript"></script>

HTML 代码:

Html代码收藏代码
  1. <styletype="text/CSS">
  2. #txtFileName{
  3. width:300px;
  4. }
  5. .btnsubmit{border-bottom:#cc4f001pxsolid;border-left:#ff90001pxsolid;border-top:#ff90001pxsolid;border-right:#cc4f001pxsolid;text-align:center;padding:2px10px;line-height:16px;background:#e36b0f;height:24px;color:#fff;font-size:12px;cursor:pointer;}
  6. </style>
  7. 上传图片:<inputtype="text"id="txtFileName"/><divid="btnUp"style="width:300px;"class=btnsubmit>浏览</div>
  8. <divid="imglist"></div>

js代 码:

Js代码收藏代码
  1. <scripttype="text/javascript">
  2. $(function(){
  3. varbutton=$('#btnUp'),interval;
  4. newAjaxUpload(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. returnfalse;
  12. }
  13. //changebuttontext,whenuserselectsfile
  14. button.text('上传中');
  15. //Ifyouwanttoallowuploadingonly1fileattime,
  16. //youcandisableuploadbutton
  17. this.disable();
  18. //Uploding->Uploading.->Uploading...
  19. interval=window.setInterval(function(){
  20. vartext=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. //enableuploadbutton
  33. this.enable();
  34. vark=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 代码

Js代码收藏代码
  1. publicvoidProcessRequest(HttpContextcontext)
  2. {
  3. context.Response.ContentType="text/plain";
  4. HttpPostedFilepostedFile=context.Request.Files[0];
  5. stringsavePath="/upload/images/";
  6. intfilelength=postedFile.ContentLength;
  7. intfileSize=163600;//150K
  8. stringfileName="-1";//返回的上传后的文件名
  9. if(filelength<=fileSize)
  10. {
  11. byte[]buffer=newbyte[filelength];
  12. postedFile.InputStream.Read(buffer,0,filelength);
  13. fileName=UploadImage(buffer,savePath,"jpg");
  14. }
  15. context.Response.Write(fileName);
  16. }
  17. publicstaticstringUploadImage(byte[]imgBuffer,stringuploadpath,stringext)
  18. {
  19. try
  20. {
  21. System.IO.MemoryStreamm=newMemoryStream(imgBuffer);
  22. if(!Directory.Exists(HttpContext.Current.Server.MapPath(uploadpath)))
  23. Directory.CreateDirectory(HttpContext.Current.Server.MapPath(uploadpath));
  24. stringimgname=CreateIDCode()+"."+ext;
  25. string_path=HttpContext.Current.Server.MapPath(uploadpath)+imgname;
  26. Imageimg=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. returnuploadpath+imgname;
  33. }
  34. catch(Exceptionex)
  35. {
  36. returnex.Message;
  37. }
  38. }
  39. publicstaticstringCreateIDCode()
  40. {
  41. DateTimeTime1=DateTime.Now.ToUniversalTime();
  42. DateTimeTime2=Convert.ToDateTime("1970-01-01");
  43. TimeSpanspan=Time1-Time2;//span就是两个日期之间的差额
  44. stringt=span.TotalMilliseconds.ToString("0");
  45. returnt;
  46. }

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表