Example #1
0
        public async Task <ActionResult> Save(IEnumerable <IFormFile> files)
        {
            // The Name of the Upload component is "files"
            if (files != null)
            {
                foreach (var file in files)
                {
                    var fileContent = ContentDispositionHeaderValue.Parse(file.ContentDisposition);

                    // Some browsers send file names with full path.
                    // We are only interested in the file name.
                    var fileName     = Path.GetFileName(fileContent.FileName.ToString().Trim('"'));
                    var physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName);

                    using (var fileStream = new FileStream(physicalPath, FileMode.Create))
                    {
                        await file.CopyToAsync(fileStream);
                    }
                    string data = _importService.ConvertCsvFileToJsonObject(physicalPath);

                    List <Artikel> artikels = JsonConvert.DeserializeObject <List <Artikel> >(data);
                    _artikelService.CreateArtikels(artikels);
                    /*Mongodb way, convert the object into a json file and then import it into the collection */
                    /*Longer but just to illustrate */

                    /*physicalPath = Path.Combine(HostingEnvironment.WebRootPath, "App_Data", fileName + ".json");
                     * System.IO.File.WriteAllText(physicalPath,data);
                     * _importService.InsertDocumentsInCollection(physicalPath);*/
                }
            }

            // Return an empty string to signify success
            return(Content(""));
        }