標籤:style http color io os 使用 java ar for
本次使用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‘,檔案上傳伺服器端執行的地址
- 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 代碼
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就是兩個日期之間的差額
- string t = span.TotalMilliseconds.ToString("0");
-
- return t;
- }
jQuery外掛程式ASP.NET應用之AjaxUpload