예제 #1
0
        // GET: OrderController/Upload
        public ActionResult Upload()
        {
            var uploadOrder = new InventoryManagementService();

            uploadOrder.SaveCsvOrders(Config.PathToSalesFile);
            return(RedirectToAction(nameof(Index)));
        }
        // GET: OrderController/Upload
        public async System.Threading.Tasks.Task <ActionResult> UploadAsync(IFormFile file)
        {
            if (file != null && file.Length > 0)
            {
                try
                {
                    var        filePath     = Path.GetTempFileName();
                    var        uploadOrder  = new InventoryManagementService();
                    int        rowsAffected = 0;
                    FileStream stream       = null;
                    using (stream = System.IO.File.Create(filePath))
                    {
                        await file.CopyToAsync(stream);
                    }

                    rowsAffected = uploadOrder.SaveCsvOrders(stream.Name);
                    if (rowsAffected > 0)
                    {
                        TempData["rowsAffected"] = rowsAffected == 1 ? "1 row was affected" : $"{rowsAffected} rows were affected";
                    }
                    else
                    {
                        TempData["rowsAffected"] = "0 rows were affected";
                    }

                    return(RedirectToAction(nameof(Index)));
                }
                catch (Exception ex)
                {
                    var Err = new CreateLogFiles();
                    Err.ErrorLog(Config.PathToData + "err.log", ex.Message);
                    Console.WriteLine("Fatal error : " + ex.Message + ", please find a complete error at ErrorLog file");
                    throw;
                }
            }
            else
            {
                ViewBag.Message = "Unable to upload file";

                var uploadOrder = new InventoryManagementService();
                uploadOrder.SaveCsvOrders(Config.PathToSalesFileWeb);

                return(RedirectToAction(nameof(Index)));
            }
        }