Exemplo n.º 1
0
        public void ReadCsvTest()
        {
            string path  = Path.Combine(testFolder, "lofts.csv");
            var    lofts = MyCsvHelper.ReadCsv <TCS_Lofting>(path);

            Assert.AreEqual(34, lofts.Count);
        }
        public JsonResult FileUpload(UploadFileModel input)
        {
            try
            {
                if (ModelState.IsValid && input.File.ContentLength > 0)
                {
                    var path = Path.Combine(Server.MapPath("~/File_Drop/"), input.File.FileName);

                    if (!System.IO.File.Exists(path))
                    {
                        var transactionList = MyCsvHelper.ReadFile(input.File.InputStream);
                        var result          = TransactionHelper.ValidateFile(ref transactionList);

                        //Inserting the record to DB using Bulk Insert
                        _db.Add(transactionList);

                        ViewBag.FileUploadStatus = "Success";

                        //Saving the file after the Insertion is successful.
                        input.File.SaveAs(path);

                        return(Json(new
                        {
                            RecordsUploaded = result.Item1,
                            RecordsSkipped = result.Item2
                        }, JsonRequestBehavior.AllowGet));
                    }

                    else
                    {
                        Response.StatusCode      = (int)HttpStatusCode.Conflict;
                        ViewBag.FileUploadStatus = "Failed";
                        return(Json("This File is already uploaded. Please select a new file", JsonRequestBehavior.AllowGet));
                    }
                }

                else
                {
                    var allErrors = ModelState.Values.SelectMany(v => v.Errors);
                    Response.StatusCode = (int)HttpStatusCode.BadRequest;
                    return(Json(allErrors));
                }
            }
            catch (Exception ex)
            {
                Response.StatusCode      = (int)HttpStatusCode.BadRequest;
                ViewBag.FileUploadStatus = "Failed";
                return(Json("Please check the file and try again"));
            }
        }