Пример #1
0
        public ResponseModel BulkUploadCRMRole(IFormFile File, int RoleFor = 1)
        {
            string         downloadFilePath    = string.Empty;
            string         bulkUploadFilesPath = string.Empty;
            bool           errorFilesaved      = false;
            bool           successFilesaved    = false;
            int            count            = 0;
            List <string>  CSVlist          = new List <string>();
            SettingsCaller _newCRM          = new SettingsCaller();
            ResponseModel  objResponseModel = new ResponseModel();
            int            statusCode       = 0;
            string         statusMessage    = "";
            DataSet        dataSetCSV       = new DataSet();
            string         fileName         = "";
            string         finalAttchment   = "";
            string         timeStamp        = DateTime.Now.ToString("ddmmyyyyhhssfff");

            string[] filesName = null;

            try
            {
                string       token        = Convert.ToString(Request.Headers["X-Authorized-Token"]);
                Authenticate authenticate = new Authenticate();
                authenticate = SecurityService.GetAuthenticateDataFromTokenCache(Cache, SecurityService.DecryptStringAES(token));

                var files = Request.Form.Files;

                if (files.Count > 0)
                {
                    for (int i = 0; i < files.Count; i++)
                    {
                        fileName += files[i].FileName.Replace(".", timeStamp + ".") + ",";
                    }
                    finalAttchment = fileName.TrimEnd(',');
                }
                var Keys = Request.Form;

                var exePath = Path.GetDirectoryName(System.Reflection
                                                    .Assembly.GetExecutingAssembly().CodeBase);
                Regex  appPathMatcher = new Regex(@"(?<!fil)[A-Za-z]:\\+[\S\s]*?(?=\\+bin)");
                var    appRoot        = appPathMatcher.Match(exePath).Value;
                string Folderpath     = appRoot + "\\" + _UploadedBulkFile;
                if (files.Count > 0)
                {
                    filesName = finalAttchment.Split(",");
                    for (int i = 0; i < files.Count; i++)
                    {
                        using (var ms = new MemoryStream())
                        {
                            files[i].CopyTo(ms);
                            var          fileBytes = ms.ToArray();
                            MemoryStream msfile    = new MemoryStream(fileBytes);
                            FileStream   docFile   = new FileStream(Folderpath + "\\" + filesName[i], FileMode.Create, FileAccess.Write);
                            msfile.WriteTo(docFile);
                            docFile.Close();
                            ms.Close();
                            msfile.Close();
                            string s = Convert.ToBase64String(fileBytes);
                            byte[] a = Convert.FromBase64String(s);
                            // act on the Base64 data
                        }
                    }
                }
                #region FilePath
                bulkUploadFilesPath = rootPath + "\\" + "BulkUpload\\UploadFiles" + "\\" + CommonFunction.GetEnumDescription((EnumMaster.FileUpload)RoleFor);
                downloadFilePath    = rootPath + "\\" + "BulkUpload\\DownloadFiles" + "\\" + CommonFunction.GetEnumDescription((EnumMaster.FileUpload)RoleFor);

                #endregion
                dataSetCSV = CommonService.csvToDataSet(Folderpath + "\\" + finalAttchment);
                CSVlist    = _newCRM.CRMRoleBulkUpload(new CRMRoleService(Cache, Db), authenticate.TenantId, authenticate.UserMasterID, RoleFor, dataSetCSV);

                #region Create Error and Succes files and  Insert in FileUploadLog

                if (!string.IsNullOrEmpty(CSVlist[0]))
                {
                    successFilesaved = CommonService.SaveFile(downloadFilePath + "\\CRMRole\\ Success" + "\\" + "CRMRoleSuccessFile.csv", CSVlist[0]);
                }

                if (!string.IsNullOrEmpty(CSVlist[1]))
                {
                    errorFilesaved = CommonService.SaveFile(downloadFilePath + "\\CRMRole\\Error" + "\\" + "CRMRoleErrorFile.csv", CSVlist[1]);
                }

                count = _newCRM.CreateFileUploadLog(new FileUploadService(Cache, Db), authenticate.TenantId, "CRMRolesBulk.csv", errorFilesaved,
                                                    "HierarchyErrorFile.csv", "HierarchySuccessFile.csv", authenticate.UserMasterID, "CRMRole",
                                                    downloadFilePath + "\\Hierarchy\\Error" + "\\" + "CRMRoleErrorFile.csv",
                                                    downloadFilePath + "\\Hierarchy\\ Success" + "\\" + "CRMRoleSuccessFile.csv", RoleFor
                                                    );
                #endregion

                statusCode                    = count > 0 ? (int)EnumMaster.StatusCode.Success : (int)EnumMaster.StatusCode.RecordNotFound;
                statusMessage                 = CommonFunction.GetEnumDescription((EnumMaster.StatusCode)statusCode);
                objResponseModel.Status       = true;
                objResponseModel.StatusCode   = statusCode;
                objResponseModel.Message      = statusMessage;
                objResponseModel.ResponseData = CSVlist.Count;
            }
            catch (Exception)
            {
                throw;
            }
            return(objResponseModel);
        }