コード例 #1
0
        public async Task <IActionResult> UploadSingleFile([FromForm] FileResource fileResource)
        {
            if (fileResource.File == null)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = "Failed posting file.",
                    Detail   = "File is null.",
                    Instance = "ACD46F17-A239-4353-92A5-0B81AA0A96E9"
                };
                return(BadRequest(problem));
            }
            try
            {
                DateTime uploadDateTime = DateTime.Now;
                int      fileExtPos     = fileResource.File.FileName.LastIndexOf(".");
                string   extension      = fileResource.File.FileName.Substring(fileExtPos);
                string   newFileName    = Guid.NewGuid() + extension;
                User     user           = await HttpContext.GetContextUser(userService)
                                          .ConfigureAwait(false);

                File file = new File(newFileName, newFileName, user, uploadDateTime);

                await fileUploader.CopyFileToDirectory(fileResource.File, newFileName);

                await fileService.AddAsync(file);

                fileService.Save();

                return(Ok(mapper.Map <File, FileResourceResult>(file)));
            } catch (FileExistException fileExistException)
            {
                ProblemDetails problem = new ProblemDetails
                {
                    Title    = fileExistException.Message,
                    Detail   = "Please rename filename.",
                    Instance = "D902F8C6-23FF-4506-B272-C757BD709464"
                };
                return(BadRequest(problem));
            }
        }