コード例 #1
0
        public async Task <IActionResult> UploadExManifestFile(int userId, FileForUploadDto fileDto, [FromQuery] ManifestFileUploadUserParams userParams)
        {
            var currentUserId = int.Parse(User.FindFirst(ClaimTypes.NameIdentifier).Value);

            if (currentUserId == 0)
            {
                return(Unauthorized());
            }

            ArrayList returnRows = new ArrayList();
            IFormFile file       = Request.Form.Files[0];

            string folderName  = "Upload";
            string webRootPath = _hostingEnvironment.WebRootPath;
            string newPath     = System.IO.Path.Combine(webRootPath, folderName);

            if (!Directory.Exists(newPath))
            {
                Directory.CreateDirectory(newPath);
            }
            if (file.Length > 0)
            {
                var uploadToAdd = new Upload();
                uploadToAdd.FileName     = fileDto.File.FileName;
                uploadToAdd.DateUploaded = DateTime.Now;
                uploadToAdd.UserId       = currentUserId;
                uploadToAdd.GuestsAdded  = 0;

                int uploadId = await _fileService.AddUpload(uploadToAdd);

                string sFileExtension = System.IO.Path.GetExtension(file.FileName).ToLower();
                string fullPath       = System.IO.Path.Combine(newPath, file.FileName);
                using (var stream = new FileStream(fullPath, FileMode.Create))
                {
                    file.CopyTo(stream);
                }

                var sheets = _fileReader.GetExcelSheets(fullPath);
                foreach (var sheet in sheets)
                {
                    var headers = _fileReader.GetExcelSheetHeaders(sheet, 2);

                    if (!headers.Contains("PERSONAL ID"))
                    {
                        continue;
                    }

                    for (int i = (sheet.FirstRowNum + 3); i <= sheet.LastRowNum; i++) //Read Excel File
                    {
                        IRow row = sheet.GetRow(i);
                        if (row == null ||
                            row.Cells.All(d => d.CellType == CellType.Blank) ||
                            row.GetCell(0).StringCellValue.IndexOf("For Official Use", StringComparison.OrdinalIgnoreCase) >= 0)
                        {
                            continue;
                        }

                        var rowForUpload = await _fileService.ParseExManifestExcelRow(row, headers, uploadId);

                        if (rowForUpload == null)
                        {
                            continue;
                        }

                        int.TryParse(Regex.Replace(sheet.SheetName, @"[^\d]", ""), out int chalkNumber);

                        if (chalkNumber != 0)
                        {
                            rowForUpload.Chalk = chalkNumber;
                        }


                        //stay buildingId is nullable
                        if (rowForUpload.Gender == null ||
                            rowForUpload.FirstName == null ||
                            rowForUpload.LastName == null ||
                            rowForUpload.UnitId == 0)
                        {
                            returnRows.Add(rowForUpload);
                        }
                        else
                        {
                            rowForUpload = await _fileService.AutoRoomDataRow(rowForUpload, userParams);

                            await _fileService.SaveFileRowAsync(rowForUpload);

                            uploadToAdd.GuestsAdded += 1;
                        }
                    }
                }

                await _fileService.SaveUpload(uploadToAdd);

                if (System.IO.File.Exists(fullPath))
                {
                    System.IO.File.Delete(fullPath);
                }
            }
            return(Ok(returnRows));
        }