วันจันทร์ที่ 18 พฤษภาคม พ.ศ. 2558
Uploading a Folder in Asp.net MVC4
View
@{ ViewBag.Title = "uploadFolder"; }Scriptupload Folder
Controllor
public ActionResult uploadFolder() { return View(); } [HttpPost] public async TaskWeb configUploadHomeReport() { try { foreach (string file in Request.Files) { var fileContent = Request.Files[file]; if (fileContent != null && fileContent.ContentLength > 0) { // get a stream var stream = fileContent.InputStream; // and optionally write the file to disk var fileName = fileContent.FileName; fileName = fileName.Replace(@"/",@"\"); string[] arr = fileName.Split('\\'); //\tculms\sub\sub1 var tempPath = string.Empty; for (int i = 0; i < arr.Length; i++) { if (!isFolder(arr[i])) { var targetPath = string.Empty; if (i == 0) { targetPath = Path.Combine(Server.MapPath("~/Images"), arr[i]); tempPath = tempPath + "\\" + arr[i]; if (!System.IO.Directory.Exists(targetPath)) { System.IO.Directory.CreateDirectory(targetPath); } } else { tempPath = tempPath + "\\" + arr[i]; targetPath = Server.MapPath("~/Images") + tempPath; //targetPath = Path.Combine(Server.MapPath("~/Images"), tempPath); if (!System.IO.Directory.Exists(targetPath)) { System.IO.Directory.CreateDirectory(targetPath); } } } } var path = Path.Combine(Server.MapPath("~/Images"), fileName); using (var fileStream = System.IO.File.Create(path)) { stream.CopyTo(fileStream); } } } } catch (Exception) { Response.StatusCode = (int)HttpStatusCode.BadRequest; return Json("Upload failed"); } return Json("File uploaded successfully"); } private bool isFolder(string txt) { var arr = txt.ToCharArray(); for (int i = 0; i < arr.Length;i++ ) { if (arr[i] == '.') { return true; } } return false; }
Result
Derivative
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
ขอบคุณครับ โค้ดนำไปใช้งานได้จริง
ตอบลบ--------------------------------------
ผมก็กำลังทำเว็บสอนเหมือนกัน ลองเข้าไปดูครับ : SA Gaming
บทความดีมากครับ
ตอบลบมืออ่านเข้าใจง่ายสามารถนำไปใช้งานได้เลย
--------------------------
ผมก็กำลังเขียนสอนเหมือนกัน เป็นเว็บสอนเล่นบาคาร่าออนไลน์
แนะนำบทความ วิธีเลือกห้องบาคาร่า