Ejemplo n.º 1
0
 public JsonResult Post([FromBody] RecordPostModel recordModel)
 {
     // Admin and Client can access
     if (AuthRepo.IsAuthenticated())
     {
         User   currentUser = AppRepo.GetUserById(AuthRepo.CurrentUserId);
         int    status;
         string msg;
         AppRepo.AddRecord(currentUser, recordModel, out status, out msg);
         Response.StatusCode = status;
         return(Json(new { Msg = msg }));
     }
     Response.StatusCode = 401;
     return(Json(new { Msg = "Unauthorized" }));
 }
Ejemplo n.º 2
0
        public void Post(IFormFile file)
        {
            if (AuthRepo.IsAuthenticated())
            {
                if (file != null && file.ContentType == "application/octet-stream" && file.FileName.Contains(".xlsx"))
                {
                    var filename = Path.GetFileNameWithoutExtension(Path.GetTempFileName()) + ".xlsx";
                    var filePath = Path.Combine(Path.GetTempPath(), filename);

                    if (file.Length > 0)
                    {
                        using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.ReadWrite))
                        {
                            file.CopyTo(stream);
                            stream.Flush();
                        }
                    }

                    FileStream streamNew = null;
                    try
                    {
                        streamNew = new FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
                    }
                    catch (Exception)
                    {
                        Response.StatusCode = 500;
                        return;
                    }

                    ExcelPackage   excelFile = new ExcelPackage(streamNew);
                    ExcelWorksheet worksheet = excelFile.Workbook.Worksheets[1];

                    if (!AppRepo.AddRecordsFromExcel(AppRepo.GetUserById(AuthRepo.CurrentUserId), worksheet))
                    {
                        Response.StatusCode = 500;
                    }

                    return;
                }
                Response.StatusCode = 415;
                return;
            }

            Response.StatusCode = 401;
        }