public ActionResult UploadFile(FormCollection form) { FilesRepo filesRepo = new FilesRepo(context); //string Username = form["txtFileName"]; //file = form.Get("profilepic"); //string Password = form["password"]; byte[] file = null; string fileContentType = null; var guid = Guid.NewGuid(); string classid = form["txtClassDeptId"]; var DeptClassId = Int16.Parse(classid); if (Request != null) { HttpPostedFileBase file1 = Request.Files["File"]; if ((file1 != null) && (file1.ContentLength > 0) && !string.IsNullOrEmpty(file1.FileName)) { string fileName = file1.FileName; fileContentType = file1.ContentType; byte[] fileBytes = new byte[file1.ContentLength]; file1.InputStream.Read(fileBytes, 0, Convert.ToInt32(file1.ContentLength)); file = fileBytes; } } var data = new Files() { FileName = form["txtFileName"], ContentType = fileContentType, file = file, CreatedBy = (string)User.Identity.GetUserId(), DeptClassId = Int16.Parse(classid), CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now }; filesRepo.Add(data); return(Redirect("/Files?DeptClassId=" + DeptClassId)); }
public ActionResult UploadFile(FormCollection form) { FilesRepo filesRepo = new FilesRepo(context); DeptRepo deptRepo = new DeptRepo(context); //string Username = form["txtFileName"]; //file = form.Get("profilepic"); //string Password = form["password"]; byte[] file = null; string fileContentType = null; var guid = Guid.NewGuid(); string classid = form["txtClassDeptId"]; var DeptClassId = Int16.Parse(classid); string fileName = null; string folderName = @"C:\Proj"; string name = deptRepo.GetName(DeptClassId); if (Request != null) { HttpPostedFileBase file1 = Request.Files["File"]; if ((file1 != null) && (file1.ContentLength > 0) && !string.IsNullOrEmpty(file1.FileName)) { fileName = file1.FileName; fileContentType = file1.ContentType; byte[] fileBytes = new byte[file1.ContentLength]; file1.InputStream.Read(fileBytes, 0, Convert.ToInt32(file1.ContentLength)); file = fileBytes; } } // To create a string that specifies the path to a subfolder under your // top-level folder, add a name for the subfolder to folderName. //\proj\className\file.pdf or tx string pathString = System.IO.Path.Combine(folderName, name); string pathString1 = System.IO.Path.Combine(pathString, form["txtFileName"]); /// create the directory System.IO.Directory.CreateDirectory(pathString); if (!System.IO.File.Exists(pathString1)) { // File.WriteAllBytes(Server.MapPath(filePath), imgArray); try { System.IO.File.WriteAllBytes(pathString1, file); }catch (Exception e) { e.ToString(); Console.WriteLine(e.ToString()); } } else { Console.WriteLine("File \"{0}\" already exists.", pathString); } var data = new Files() { FileName = form["txtFileName"], ContentType = fileContentType, FilePathDirectory = pathString1, file = file, CreatedBy = (string)User.Identity.GetUserId(), DeptClassId = Int16.Parse(classid), CreatedOn = DateTime.Now, ModifiedOn = DateTime.Now }; filesRepo.Add(data); return(Redirect("/Files?DeptClassId=" + DeptClassId)); }