コード例 #1
0
ファイル: UploadController.cs プロジェクト: eosishtar/NetCore
        public ActionResult <UploadCreateResultModel> UploadImage()
        {
            //TODO:

            /*
             *
             * Add Serilog
             * Add MiddleWare for the logging
             */

            if (Request.Form.Files.Count == 0)
            {
                return(this.HandleBadRequest <UploadCreateResultModel>(
                           Request.Path,
                           "There are no images to upload"));
            }

            int fileCount = 0;

            try
            {
                foreach (var file in Request.Form.Files)
                {
                    if (file.Length > 0)
                    {
                        fileCount++;

                        Document document = new Document()
                        {
                            Id       = Guid.NewGuid(),
                            FileName = file.FileName
                        };

                        MemoryStream ms = new MemoryStream();
                        file.CopyTo(ms);
                        document.FileData = ms.ToArray();

                        ms.Close();
                        ms.Dispose();

                        _documentInteractor.UploadDocument(document);
                    }
                }

                return(new UploadCreateResultModel()
                {
                    StatusCode = (int)HttpStatusCode.OK,
                    Message = $"'{fileCount}' file(s) were successfully uploaded."
                });
            }
            catch (Exception ex)
            {
                //TODO: need to do some logging here and middleware
                return(this.HandleBadRequest <UploadCreateResultModel>(
                           Request.Path,
                           ex.Message.ToString()));
            }
        }
コード例 #2
0
ファイル: HomeController.cs プロジェクト: eosishtar/NetCore
        public IActionResult UploadDocument(DocumentViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(RedirectToAction("Create", "Home", model));
            }

            try
            {
                foreach (var file in Request.Form.Files)
                {
                    if (file.Length > 0)
                    {
                        var document = new NetCore.Core.Entities.Document()
                        {
                            Id          = Guid.NewGuid(),
                            Description = model.FileName,
                            FileName    = file.FileName
                        };

                        MemoryStream ms = new MemoryStream();
                        file.CopyTo(ms);
                        document.FileData = ms.ToArray();

                        ms.Close();
                        ms.Dispose();

                        _documentInteractor.UploadDocument(document);

                        ViewBag.Message = "Image(s) stored in database!";
                    }
                }
            }
            catch (Exception ex)
            {
                ViewBag.Message = "An error has occurred. <br /> " + ex.Message.ToString();
            }

            return(RedirectToAction("Index", model));
        }