標籤:
前台js代碼 匯入jQuery上傳外掛程式Uploadify 3.2
1 $("#photofile").uploadify({//上傳電子相片 2 swf: ‘/Theme/NewBlueVacation/images/uploadify.swf‘, 3 uploader: ‘/ExtendClass/UploadFiles.ashx‘, //幕後處理檔案的路徑 4 width: 85, 5 height: 21, 6 buttonText: ‘ ‘,//上傳按鈕文字 7 fileSizeLimit: ‘20KB‘, 8 multi: false, 9 buttonImage: "/Theme/NewBlueVacation/images/ManualRecharge_tubiao06.png",//上傳按鈕路徑10 fileTypeExts: ‘*.jpg‘,//;*.png;*.gif;*.bmp;*.doc;*.docx11 fileTypeDesc: ‘圖片檔案‘,12 onSelectError: function (file, errorCode, errorMsg) {13 switch (errorCode) {14 case -100:15 layer.alert("上傳的檔案數量已經超出系統限制的" + $(‘#photofile‘).uploadify(‘settings‘, ‘queueSizeLimit‘) + "個檔案!");16 break;17 case -110:18 layer.alert("檔案 [" + file.name + "] 大小超出系統限制的" + $(‘#photofile‘).uploadify(‘settings‘, ‘fileSizeLimit‘) + "大小!");19 break;20 case -120:21 layer.alert("檔案 [" + file.name + "] 大小異常!");22 break;23 case -130:24 layer.alert("檔案 [" + file.name + "] 類型不正確!");25 break;26 }27 },28 onUploadSuccess: function (file, data, response) {29 if (data == "0") {30 $.messager.alert("提示", "上傳失敗!", ‘error‘);31 }32 else {33 $("<img/>").attr("src", data).load(function () {34 /*35 如果要擷取圖片的真實的寬度和高度有三點必須注意36 1、需要建立一個image對象:如這裡的$("<img/>")37 2、指定圖片的src路徑38 3、一定要在圖片載入完成後執行如.load()函數裡執行39 */40 realWidth = this.width;41 realHeight = this.height;42 //如果真實的寬度大於瀏覽器的寬度就按照100%顯示43 if (realWidth != 358) {44 this.width = 358;45 }46 else {//如果小於瀏覽器的寬度按照原尺寸顯示47 //$(img).css("width", realWidth + ‘px‘).css("height", realHeight + ‘px‘);48 }49 if (realHeight != 441)50 {51 this.height=441;52 }53 });54 //$(‘#photopic‘).attr(‘src‘, data);55 $(‘#photofilename‘).val(data);56 $(‘#phototext‘).val(file.name);57 }58 }59 });js代碼
後台C#代碼
1 public void ProcessRequest(HttpContext context) 2 { 3 context.Response.ContentType = "text/plain"; 4 HttpPostedFile file = context.Request.Files["Filedata"]; 5 string Dir = "Upload"; 6 string uploadPath = "\\" + Dir + "\\TravelProduct\\" + DateTime.Now.ToString("yyyyMMdd") + "\\"; 7 string returnPath = uploadPath.Replace("\\", "/"); 8 uploadPath = HttpContext.Current.Server.MapPath(uploadPath); 9 if (file != null)10 {11 if (!Directory.Exists(uploadPath))12 {13 Directory.CreateDirectory(uploadPath);14 }15 string fileType = Tool.MakeRandomNumber(8, 1) + Tool.GetPicType(file.FileName);16 file.SaveAs(uploadPath + fileType);17 //下面這句代碼缺少的話,上傳成功後上傳隊列的顯示不會自動消失18 context.Response.Write(returnPath + fileType);19 }20 else21 {22 context.Response.Write("0");23 }24 }25 #region 取指定檔案的尾碼名26 /// <summary>27 /// 取指定檔案的尾碼名28 /// </summary>29 /// <param name="PicName">檔案名稱或檔案路徑</param>30 /// <returns></returns>31 public static string GetPicType(string PicName)32 {33 return Path.GetExtension(PicName);34 }35 #endregion上傳檔案的後台代碼
.net檔案上傳的方法