日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

jQuery插件ASP.NET应用之AjaxUpload

發(fā)布時(shí)間:2023/12/9 asp.net 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 jQuery插件ASP.NET应用之AjaxUpload 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本次使用AJAXUPLOAD做為上傳客戶端無刷上傳插件,其最新版本為3.9,官方地址:http://valums.com/ajax-upload/

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

?

Html代碼??
  • <script?src="Scripts/jquery-1.4.2.min.js"?type="text/javascript"></script>??
  • <script?src="Scripts/ajaxupload3.9.js"?type="text/javascript"></script>??
  • ?HTML 代碼:

    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代 碼:

    Js代碼??
  • <script?type="text/javascript">??
  • ??
  • ????$(function?()?{??
  • ????????var?button?=?$('#btnUp'),?interval;??
  • ????????new?AjaxUpload(button,?{??
  • ????????????//action:?'upload-test.php',文件上傳服務(wù)器端執(zhí)行的地址??
  • ????????????action:?'/handler/AjaxuploadHandler.ashx',??
  • ????????????name:?'myfile',??
  • ????????????onSubmit:?function?(file,?ext)?{??
  • ????????????????if?(!(ext?&&?/^(jpg|jpeg|JPG|JPEG)$/.test(ext)))?{??
  • ????????????????????alert('圖片格式不正確,請(qǐng)選擇?jpg?格式的文件!',?'系統(tǒng)提示');??
  • ????????????????????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?服務(wù)器端傳回的信息??
  • ????????????????button.text('上傳圖片(只允許上傳JPG格式的圖片,大小不得大于150K)');??
  • ??????????????????
  • ????????????????window.clearInterval(interval);??
  • ??
  • ????????????????//?enable?upload?button??
  • ????????????????this.enable();??
  • ??
  • ????????????????var?k?=?response.replace("<PRE>",?"").replace("</PRE>",?"");??
  • ??
  • ????????????????if?(k?==?'-1')?{??
  • ????????????????????alert('您上傳的文件太大啦!請(qǐng)不要超過150K');??
  • ????????????????}??
  • ????????????????else?{??
  • ????????????????????alert("服務(wù)器端傳回的串:"+k);??
  • ????????????????????alert("本地文件名稱:"+file);??
  • ????????????????????$("#txtFileName").val(k);??
  • ????????????????????$("<img?/>").appendTo($('#imglist')).attr("src",?k);??
  • ????????????????}??
  • ????????????}??
  • ????????});??
  • ??
  • ????});??
  • </script>??
  • ?服務(wù)器端 ajaxuploadhandler.ashx 代碼

    Js代碼??
  • 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就是兩個(gè)日期之間的差額?????
  • ????????????string?t?=?span.TotalMilliseconds.ToString("0");??
  • ??
  • ????????????return?t;??
  • ????????} ?
  • 總結(jié)

    以上是生活随笔為你收集整理的jQuery插件ASP.NET应用之AjaxUpload的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。