Ext Net 1 x Ext Net FileUpload上傳檔案
阿新 • • 發佈:2018-12-21
今天在使用ext.net的UploadField控制元件想上傳檔案時,發現examples.ext.net官網上的例子寫的不是很詳細。於是通過網上找資料,結合asp.net的檔案上傳的方法,終於實現了圖片的上傳功能。以下就是實現的程式碼,供大家參考!
首先在.aspx檔案中插入一個檔案上傳的控制元件:
<ext:FileUploadField ID="UploadFile" runat="server" FieldLabel="上傳附件" Width="300" ButtonText="瀏覽..." Icon="Attach" />
然後是.cs檔案中實現上傳的具體程式碼:
/// <summary> /// 上傳附件 /// </summary> protected void Upload(object sender, DirectEventArgs e) { string UploadFile = ""; if (this.UploadFile.HasFile) { UploadFile = this.UploadFile.PostedFile.FileName.ToString(); int FileSize = Int32.Parse(this.UploadFile.PostedFile.ContentLength.ToString()); if (FileSize > 5 * 1024 * 1024) { X.Msg.Alert("提示資訊", "上傳檔案過大!").Show(); return; } string strFileName = Path.GetExtension(this.UploadFile.PostedFile.FileName).ToUpper();//獲取檔案字尾 if (!(strFileName == ".BMP" || strFileName == ".GIF" || strFileName == ".JPG")) { X.Msg.Alert("提示資訊", "檔案格式不正確!").Show(); return; } Random ran = new Random(); string sNewName = DateTime.Now.ToString(@"yyyyMMddHHmmss") + ran.Next(100, 999) + Path.GetExtension(this.UploadFile.PostedFile.FileName); string strPath = Server.MapPath("~/Uploads/ZmQuo/"+System.DateTime.Now .Year .ToString () +"/"+ sNewName); if (!Directory.Exists(Path.GetDirectoryName(strPath))) { Directory.CreateDirectory(Path.GetDirectoryName(strPath)); } try { this.UploadFile.PostedFile.SaveAs(strPath); X.Msg.Alert("提示資訊", "檔案上傳成功!").Show(); return; } catch(Exception ex) { throw ex; } } }