Esempio n. 1
0
        public async Task <IActionResult> Upload()
        {
            var result = new List <Guid>();

            var files = HttpContext.Request.Form.Files;

            foreach (var file in files)
            {
                if (file.Length > 0)
                {
                    var fileEntity = new Model.Models.File
                    {
                        Type = Path.GetExtension(file.ContentDisposition)
                    };

                    using (var memoryStream = new MemoryStream())
                    {
                        await file.CopyToAsync(memoryStream);

                        fileEntity.Path = memoryStream.ToArray();
                    }

                    _context.Files.Add(fileEntity);
                    await _context.SaveChangesAsync();

                    result.Add(fileEntity.Id);
                }
            }

            TempData.Add(Constants.UploadedFilesTempDataKey, result);

            return(Ok());
        }
Esempio n. 2
0
        /// <summary>
        /// A parameterkent kapott Filet exceltablakent dolgozza fel.
        /// </summary>
        /// <param name="file">Feldolgozando File</param>
        /// <returns>Az excel fajl cellai</returns>
        private List <List <String> > GetExcelDataFromFile(Model.Models.File file)
        {
            List <List <string> > excelData = new List <List <string> >();

            using (MemoryStream stream = new MemoryStream(file.Data))
                using (ExcelPackage excelPackage = new ExcelPackage(stream))
                {
                    foreach (ExcelWorksheet worksheet in excelPackage.Workbook.Worksheets)
                    {
                        for (int i = worksheet.Dimension.Start.Row; i <= worksheet.Dimension.End.Row; i++)
                        {
                            List <string> rowData = new List <string>();

                            for (int j = worksheet.Dimension.Start.Column; j <= worksheet.Dimension.End.Column; j++)
                            {
                                if (worksheet.Cells[i, j].Value != null)
                                {
                                    rowData.Add(worksheet.Cells[i, j].Value.ToString());
                                }
                            }

                            excelData.Add(rowData);
                        }
                    }
                }

            return(excelData);
        }
        public async Task <IActionResult> Create(List <IFormFile> photoFile, [Bind("FirstName,LastName,BirthDate,Photo,Id,CreatedOn,ModifiedOn")] Author author)
        {
            if (ModelState.IsValid)
            {
                // Upload du fichier de la photo
                if (photoFile.Count != 0)
                {
                    var file       = photoFile.FirstOrDefault();
                    var fileEntity = new Model.Models.File
                    {
                        Type = Path.GetExtension(file.ContentDisposition)
                    };

                    using (var memoryStream = new MemoryStream())
                    {
                        await file.CopyToAsync(memoryStream);

                        fileEntity.Path = memoryStream.ToArray();
                    }

                    _context.Files.Add(fileEntity);
                    await _context.SaveChangesAsync();

                    author.PhotoId = fileEntity.Id;
                }

                _context.Add(author);
                await _context.SaveChangesAsync();

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
Esempio n. 4
0
        public HttpResponseMessage UpdateFile(HttpRequestMessage request, FileViewModel fileViewModel)
        {
            return(CreateHttpRespond(request, () =>
            {
                var fullPath = HttpContext.Current.Server.MapPath("~/" + fileViewModel.Path);

                if (System.IO.File.Exists(fullPath))
                {
                    var folder = _newCategoryService.GetByID(fileViewModel.NewCategoryID);
                    string pathDest = HttpContext.Current.Server.MapPath("~/UploadedFiles/FilePdf/" + folder.Alias + "-" + folder.ID);
                    //if (System.IO.File.Exists(pathDest + "/" + fileViewModel.Name))
                    //{
                    //    return Request.CreateResponse(HttpStatusCode.NotAcceptable, "File này đã tồn tại trong danh mục " + folder.Name );
                    //}
                    if (!Directory.Exists(pathDest))
                    {
                        Directory.CreateDirectory(pathDest);
                    }
                    System.IO.File.Move(fullPath, pathDest + "/" + fileViewModel.Name);

                    var file = new Model.Models.File();
                    file.UpdateFile(fileViewModel);

                    file.TimeStarted = file.TimeStarted.AddDays(1);
                    file.Path = CommonConstants.FileUpload + folder.Alias + "-" + folder.ID + "/" + file.Name;
                    file.UpdatedDate = DateTime.Now;

                    _fileService.Update(file);
                    _fileService.Save();
                }
                return Request.CreateResponse(HttpStatusCode.OK, "Update thành công.");
            }));
        }
        public async Task <IActionResult> Edit(List <IFormFile> photoFile, Guid id, [Bind("FirstName,LastName,BirthDate,Photo,Id,CreatedOn,ModifiedOn")] Author author)
        {
            if (id != author.Id)
            {
                return(NotFound());
            }

            if (ModelState.IsValid)
            {
                try
                {
                    // Upload du fichier de la photo
                    if (photoFile.Count != 0)
                    {
                        var file       = photoFile.FirstOrDefault();
                        var fileEntity = new Model.Models.File
                        {
                            Type = Path.GetExtension(file.ContentDisposition)
                        };

                        using (var memoryStream = new MemoryStream())
                        {
                            await file.CopyToAsync(memoryStream);

                            fileEntity.Path = memoryStream.ToArray();
                        }

                        _context.Files.Add(fileEntity);
                        await _context.SaveChangesAsync();

                        author.PhotoId = fileEntity.Id;

                        // TODO: Suppression de la photo précédente (si existante) ?
                    }

                    _context.Update(author);
                    await _context.SaveChangesAsync();
                }
                catch (DbUpdateConcurrencyException)
                {
                    if (!AuthorExists(author.Id))
                    {
                        return(NotFound());
                    }
                    else
                    {
                        throw;
                    }
                }

                return(RedirectToAction(nameof(Index)));
            }
            return(View(author));
        }
Esempio n. 6
0
        public async Task <IActionResult> Create(List <IFormFile> couvertureFile, [Bind("Title,ISBN,Cycle,Collection,Note,PublicationDate,Id,CreatedOn,ModifiedOn,GenreId,ScenaristeId,DessinateurId,EditorId")] Comic comic)
        {
            if (ModelState.IsValid)
            {
                // Gestion des fichiers uploadés via le Drag'n'Drop
                if (TempData.ContainsKey(Constants.UploadedFilesTempDataKey))
                {
                    var filesId = (List <Guid>)TempData[Constants.UploadedFilesTempDataKey];
                    comic.FileId = filesId.FirstOrDefault();
                }

                // Upload du fichier de la couverture
                if (couvertureFile.Count != 0)
                {
                    var file = couvertureFile.FirstOrDefault();

                    var fileEntity = new Model.Models.File
                    {
                        Type = Path.GetExtension(file.ContentDisposition)
                    };

                    using (var memoryStream = new MemoryStream())
                    {
                        await file.CopyToAsync(memoryStream);

                        fileEntity.Path = memoryStream.ToArray();
                    }

                    _context.Files.Add(fileEntity);
                    await _context.SaveChangesAsync();

                    comic.CouvertureId = fileEntity.Id;
                }

                _context.Add(comic);
                await _context.SaveChangesAsync();

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

            ViewBag.Authors = _context.Authors.ToList();
            ViewBag.Editors = _context.Editors.ToList();
            ViewBag.Genres  = _context.Genres.ToList();

            return(View(comic));
        }