public string UploadFile(string _sFolder, HttpPostedFile _File) { string str = ""; string fileName = Path.GetFileName(_File.FileName); string str3 = fileName.Substring(fileName.LastIndexOf(".") + 1); string str4 = UnicodeUtility.UnicodeToKoDauAndGach(HttpUtility.HtmlDecode(fileName.Substring(0, fileName.LastIndexOf(".")))); if (!Directory.Exists(_sFolder)) { Directory.CreateDirectory(_sFolder); } try { int num = 2; if (!File.Exists(_sFolder + str4 + "." + str3)) { goto Label_012B; } goto Label_0084; Label_007C: num++; Label_0084 :; if (File.Exists(string.Concat(new object[] { _sFolder, str4, num, ".", str3 }))) { goto Label_007C; } _File.SaveAs(string.Concat(new object[] { _sFolder, str4, num, ".", str3 })); return(string.Concat(new object[] { str4, num, ".", str3 })); Label_012B: if ("jpg,gif,png,bmp".IndexOf(str3.ToLower()) > 0) { int fileSize = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["fileSize"]); int fileWidth = Convert.ToInt32(System.Configuration.ConfigurationManager.AppSettings["fileWidth"]); System.Drawing.Image img = System.Drawing.Image.FromStream(_File.InputStream); if (_File.ContentLength < (fileSize * 1024) && img.Width < fileWidth) { _File.SaveAs(_sFolder + str4 + "." + str3); str = str4 + "." + str3; } else { throw new Exception("Kích thước ảnh lớn hơn cho phép!"); } } else { _File.SaveAs(_sFolder + str4 + "." + str3); str = str4 + "." + str3; } } catch (Exception exception) { EventLog.WriteEntry("UploadFile1", exception.ToString(), EventLogEntryType.Error); } return(str); }
private void UploadFile(FileUpload fu, string targetFolder) { if (!targetFolder.EndsWith("\\")) { targetFolder += "\\"; } string contentType = fu.PostedFile.ContentType; if (contentType == "application/octet-stream") { if (fu.FileName.IndexOf(".flv") == -1) { ClientScript.RegisterClientScriptBlock(GetType(), "alert", "<script>alert('" + fu.FileName + " không hợp lệ');</script>"); return; } } else if (contentType.IndexOf("image") == -1 && contentType.IndexOf("audio") == -1 && contentType.IndexOf("video") == -1 && contentType.IndexOf("flash") == -1) { ClientScript.RegisterClientScriptBlock(GetType(), "alert", "<script>alert('" + fu.FileName + " không hợp lệ');</script>"); return; } if (fu.FileName.IndexOf(".") == -1) { return; } string extension = fu.FileName.Substring(fu.FileName.IndexOf(".") + 1); string name = UnicodeUtility.UnicodeToKoDauAndGach(HttpUtility.HtmlDecode(fu.FileName.Substring(0, fu.FileName.IndexOf(".")))); int i = 2; if (File.Exists(targetFolder + fu.FileName)) { while (File.Exists(targetFolder + name + i + "." + extension)) { i++; } fu.SaveAs(targetFolder + name + i + "." + extension); } else { fu.SaveAs(targetFolder + fu.FileName); } }
protected void btnNewFolder_Click(object sender, EventArgs e) { string path = Server.MapPath("~/Images2018/Uploaded/" + Crypto.DecryptFromHTML(postBackArg.Value)); string newFolderName = UnicodeUtility.UnicodeToKoDauAndGach(HttpUtility.HtmlDecode(postBackArg2.Value)); if (!Directory.Exists(path + "\\" + newFolderName)) { Directory.CreateDirectory(path + "\\" + newFolderName); // bind treeview ltrFolder.Text = getFolder(); ltrFolder.Text += string.Format("<li>{0}</li>", buildNode("Common", "Common")); // bind gridview Repeater1.DataSource = getTableData(postBackArg.Value, txtKeyword.Text.Trim(), 0); Repeater1.DataBind(); } else { Page.ClientScript.RegisterClientScriptBlock(this.GetType(), "alert", "<script>alert('Thư mục đã tồn tại');</script>"); } }