private string PrepareAndSaveFile(HttpPostedFileBase file, string formerFilePath) { try { const string mainPath = "~/Stores/Uploads"; var subPath = HostingEnvironment.MapPath(mainPath); if (string.IsNullOrEmpty(subPath)) { return(null); } if (!Directory.Exists(subPath)) { Directory.CreateDirectory(subPath); var dInfo = new DirectoryInfo(subPath); var dSecurity = dInfo.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); dInfo.SetAccessControl(dSecurity); } var path = ""; if (!SaveToFolder(file, ref path, subPath, formerFilePath)) { return(""); } return(PhysicalToVirtualPathMapper.MapPath(path).Replace("~", "")); } catch (Exception ex) { ErrorLogger.LogError(ex.StackTrace, ex.Source, ex.Message); return(string.Empty); } }
public string SaveFile2(string formerPath, HttpPostedFileBase file, long importerId) { try { if (file != null && file.ContentLength > 0) { var mainPath = HostingEnvironment.MapPath("~/ImportDocuments/" + importerId.ToString(CultureInfo.InvariantCulture)); if (string.IsNullOrEmpty(mainPath)) { return(""); } if (!Directory.Exists(mainPath)) { Directory.CreateDirectory(mainPath); var dInfo = new DirectoryInfo(mainPath); var dSecurity = dInfo.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); dInfo.SetAccessControl(dSecurity); } var path = ""; if (SaveToFolder(file, ref path, mainPath, formerPath)) { return(PhysicalToVirtualPathMapper.MapPath(path)); } } return(string.Empty); } catch (Exception ex) { ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message); return(string.Empty); } }
public ActionResult SaveTempFile(HttpPostedFileBase file) { var gVal = new GenericValidator(); try { var importerInfo = GetLoggedOnUserInfo(); if (importerInfo.Id < 1) { gVal.Code = -1; gVal.Error = "Your session has timed out."; return(Json(gVal, JsonRequestBehavior.AllowGet)); } var path = PrepareAndSaveFile(file, importerInfo.Id, ""); if (string.IsNullOrEmpty(path)) { gVal.Code = -1; gVal.Error = "Document information Could not be saved."; return(Json(gVal, JsonRequestBehavior.AllowGet)); } gVal.Path = PhysicalToVirtualPathMapper.MapPath(path).Replace("~", ""); gVal.Code = 5; return(Json(gVal, JsonRequestBehavior.AllowGet)); } catch (Exception ex) { ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message); gVal.Code = -1; return(Json(gVal, JsonRequestBehavior.AllowGet)); } }
private string SaveFile(HttpPostedFileBase file, string folderPath) { try { if (file != null && file.ContentLength > 0) { var mainPath = Server.MapPath(folderPath + "/Assets"); if (!Directory.Exists(mainPath)) { Directory.CreateDirectory(mainPath); var dInfo = new DirectoryInfo(mainPath); var dSecurity = dInfo.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); dInfo.SetAccessControl(dSecurity); } var path = ""; if (SaveToFolder(file, ref path, mainPath, "")) { return(PhysicalToVirtualPathMapper.MapPath(path)); } } return(string.Empty); } catch (Exception ex) { ErrorLogger.LogError(ex.StackTrace, ex.Source, ex.Message); return(string.Empty); } }
private string MoveFile(long importerId, string tempPath) { try { var path = HostingEnvironment.MapPath("~/StandardRequirements/" + importerId.ToString(CultureInfo.InvariantCulture)); if (string.IsNullOrEmpty(path)) { return(""); } var tmpPath = "~/tempFiles/" + importerId.ToString(CultureInfo.InvariantCulture); var mappedTmpPath = HostingEnvironment.MapPath(tmpPath); if (string.IsNullOrEmpty(mappedTmpPath)) { return(""); } if (!Directory.Exists(path)) { Directory.CreateDirectory(path); var dInfo = new DirectoryInfo(path); var dSecurity = dInfo.GetAccessControl(); dSecurity.AddAccessRule(new FileSystemAccessRule(new SecurityIdentifier(WellKnownSidType.WorldSid, null), FileSystemRights.FullControl, InheritanceFlags.ObjectInherit | InheritanceFlags.ContainerInherit, PropagationFlags.NoPropagateInherit, AccessControlType.Allow)); dInfo.SetAccessControl(dSecurity); } var fpth = HostingEnvironment.MapPath(tempPath); var fileName = Path.GetFileName(fpth); if (string.IsNullOrEmpty(fileName)) { return(""); } var newPathv = Path.Combine(path, fileName); System.IO.File.Copy(fpth, newPathv); var dir = new DirectoryInfo(mappedTmpPath); var files = dir.GetFiles(); if (files.Any()) { files.ForEach(j => { System.IO.File.Delete(j.FullName); }); } return(PhysicalToVirtualPathMapper.MapPath(newPathv).Replace("~", "")); } catch (Exception ex) { ErrorLogger.LoggError(ex.StackTrace, ex.Source, ex.Message); return(string.Empty); } }