.net檔案上傳的方法

來源:互聯網
上載者:User

標籤:

前台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檔案上傳的方法

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.