Exemplo n.º 1
0
        public async Task <IActionResult> Post(List <IFormFile> files)
        {
            //Alternate: Based on IHostingEnvironment
            //Debug: Check the string values
            //string webRootPath = _env.WebRootPath;
            //string contentRootPath = _env.ContentRootPath;
            //return Content(webRootPath + "\n" + contentRootPath);

            long size = files.Sum(f => f.Length);

            // full path to file in temp location
            var filePath = Path.GetTempFileName();

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    //using (var stream = new FileStream(filePath, FileMode.Create))
                    var fileName = ContentDispositionHeaderValue
                                   .Parse(file.ContentDisposition)
                                   .FileName
                                   .Trim('"');     // FileName returns "fileName.ext"(with double quotes) in beta 3

                    if (fileName.EndsWith(".txt")) // Important for security if saving in webroot
                    {
                        filePath = _env.ContentRootPath + "\\Uploads\\" + fileName;
                        using (var targetStream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(targetStream);
                        }
                    }
                    else if (fileName.EndsWith(".csv"))    // Comma Seperated Values
                    {
                        filePath = _env.ContentRootPath + "\\Uploads\\" + fileName;
                        using (var targetStream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(targetStream);
                        }
                    }
                    else if (fileName.EndsWith(".xls"))    // Excel 2003 file
                    {
                        filePath = _env.ContentRootPath + "\\Uploads\\" + fileName;
                        using (var targetStream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(targetStream);
                        }


                        //List<inMF> sectionList = new List<inMF>();
                        var excelData = new ReadExcel(fileName);
                        //Code to record section txt (Phase I)
                        var sectiontxt = excelData.getData("Sheet1");

                        //Initialize method variables
                        int recordNr = 0;
                        int Depth    = 0;
                        //int cntChild = 1;
                        int[] captureParent = new int[15];
                        int[,] valParent = new int[10, captureParent.Max()];
                        valParent[1, 0]  = 1;
                        valParent[1, 1]  = 1;

                        Pack Spec = new Pack();

                        if (ModelState.IsValid)
                        {
                            foreach (var row in sectiontxt)
                            {
                                recordNr = recordNr + 1;
                                var sText = row["phrsTRtxt"].ToString();
                                int plvl  = int.Parse(row["parLevel"].ToString());

                                if (plvl == Depth)     // That means we advance on same list
                                {
                                    Spec.AddNLine(recordNr, plvl, captureParent[plvl - 1], sText);
                                }

                                if (plvl < Depth)                      // That means we moved to a higher level on nested list
                                {
                                    for (int i = Depth; i > plvl; i--) //Flush medieval values to 0
                                    {
                                        captureParent[i] = 0;
                                    }
                                }

                                Depth = Depth - (Depth - plvl);                                 //Find the level on nested list
                                Spec.AddNLine(recordNr, plvl, captureParent[Depth - 1], sText); //Writeline the ID of previous parent

                                if (plvl > Depth)                                               // That means we moved to a lower level on nested list
                                {
                                    captureParent[plvl] = recordNr;                             // Capture ID of new parent -in case a new nested list exists
                                }
                                Spec.AddNLine(recordNr, plvl, captureParent[Depth], sText);     //Writeline the ID of new parent
                                Depth = Depth + (plvl - Depth);                                 //Determine new depth

                                {
                                    sText = row["Title"].ToString();
                                    var secNo     = row["Section"].ToString();
                                    var newPhrase = new Phrase {
                                        phrsENtxt = sText, phrsTRtxt = "", phrsComp = 50, phrsReliance = 100
                                    };
                                    context.phraseSet.Add(newPhrase);
                                    context.SaveChanges();
                                    var newSection = new InMF {
                                        secNr = secNo, sectionID = newPhrase.phrsID
                                    };
                                    context.inMFSet.Add(newSection);
                                    context.SaveChanges();
                                }
                            }
                            ;
                            //isSavedSuccessfully = true;
                        }

                        {
                            //Log the error (uncomment dex variable name and add a line here to write a log.
                            ModelState.AddModelError("", "Unable to save changes. Try again, and if the problem persists see your system administrator.");
                        }
                    }

                    else if (fileName.EndsWith(".xlsx"))    // Excel 2010 file
                    {
                        filePath = _env.ContentRootPath + "\\Uploads\\" + fileName;
                        using (var targetStream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(targetStream);
                        }
                    }
                    else if (fileName.EndsWith(".doc"))    // Word 2003 file
                    {
                        filePath = _env.ContentRootPath + "\\Uploads\\" + fileName;
                        using (var targetStream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(targetStream);
                        }
                    }
                    else if (fileName.EndsWith(".docx"))    // Word 2010 file
                    {
                        filePath = _env.ContentRootPath + "\\Uploads\\" + fileName;
                        using (var targetStream = System.IO.File.Create(filePath))
                        {
                            await file.CopyToAsync(targetStream);
                        }
                    }
                }
            }
            return(Ok(new { count = files.Count, size, filePath }));
        }