public string DownloadZipFile(MediaLocation mediaLocation, string csvfileName, long userId, ILogger logger) { var csvFilePath = mediaLocation.PhysicalPath + csvfileName; var fileName = string.Empty; try { var isPinRequired = false; var user = _userRepository.GetUser(userId); var orgRoles = _organizationRoleUserRepository.GetOrganizationRoleUserCollectionforaUser(userId); var defaultRole = orgRoles.FirstOrDefault(oru => oru.RoleId == (long)user.DefaultRole); if (defaultRole != null) { Role role = _roleRepository.GetByRoleId(defaultRole.RoleId); isPinRequired = role.IsPinRequired; } var password = ""; if (isPinRequired) { var userSetting = _loginSettingRepository.Get(userId); if (userSetting != null) { password = userSetting.DownloadFilePin; } } string zipFilePath = _zipHelper.CreateZipOfSingleFile(csvFilePath, password); fileName = Path.GetFileName(zipFilePath); if (fileName == null || fileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1) { throw new InvalidFileNameException(); } } finally { try { DirectoryOperationsHelper.Delete(csvFilePath); } catch (Exception ex) { logger.Error("exception Message : " + ex.Message + " Stack Trace :" + ex.StackTrace); } } return(fileName); }
protected void DownloadZipFile(MediaLocation mediaLocation, string csvfileName) { var csvFilePath = mediaLocation.PhysicalPath + csvfileName; var response = Response; try { if (_sessionContext == null || _sessionContext.UserSession == null) { _logger.Error("User Session is null while downloading report file: " + csvFilePath); throw new Exception(); } var userId = _sessionContext.UserSession.UserId; var isPinRequired = false; var user = _userRepository.GetUser(userId); var orgRoles = _organizationRoleUserRepository.GetOrganizationRoleUserCollectionforaUser(userId); var defaultRole = orgRoles.FirstOrDefault(oru => oru.RoleId == (long)user.DefaultRole); if (defaultRole != null) { Role role = _roleRepository.GetByRoleId(defaultRole.RoleId); isPinRequired = role.IsPinRequired; } var password = ""; if (isPinRequired) { var userSetting = _loginSettingRepository.Get(userId); if (userSetting != null) { password = userSetting.DownloadFilePin; } } var zipFilePath = _zipHelper.CreateZipOfSingleFile(csvFilePath, password); var fileName = Path.GetFileName(zipFilePath); if (fileName == null || fileName.IndexOfAny(Path.GetInvalidFileNameChars()) != -1) { throw new InvalidFileNameException(); } response.Clear(); response.ClearHeaders(); response.ContentType = "application/zip"; response.AddHeader("content-disposition", "attachment; filename=" + HttpUtility.HtmlEncode(fileName.Replace(Environment.NewLine, ""))); response.Cache.SetCacheability(HttpCacheability.NoCache); var buffer = DirectoryOperationsHelper.ReadAllBytes(zipFilePath); response.BinaryWrite(buffer); } catch (Exception ex) { _logger.Error("Error while creating zip file. CSV File Name : " + csvFilePath + ". Message: " + ex.Message + " \n\t Stack Trace:" + ex.StackTrace); } finally { try { DirectoryOperationsHelper.Delete(csvFilePath); } catch (Exception exception) { _logger.Error("Error while deleting file. Name : " + csvFilePath + ". Message: " + exception.Message + " \n\t Stack Trace:" + exception.StackTrace); } response.End(); } }