コード例 #1
0
        public async Task <ActionResult <UploadResult> > UploadFiles([FromForm] List <IFormFile> files)//([FromForm]UploadedFile filesContent)
        {
            Dictionary <string, string>         errors     = new Dictionary <string, string>();
            List <(string Name, Stream Stream)> filesToAdd = new List <(string Name, Stream Stream)>();

            long size = 0;

            files = new List <IFormFile>(files ?? (IEnumerable <IFormFile>)Request.Form.Files);


            foreach (var file in files)
            {
                var realFileName = Path.GetFileName(file.FileName);
                var(ok, error) = await _fileManager.CanAdd(realFileName);

                if (!ok)
                {
                    errors[file.FileName] = error;
                    continue;
                }

                try
                {
                    filesToAdd.Add((realFileName, file.OpenReadStream()));
                }
                catch (Exception e)
                {
                    _logger.LogError(e, "Error on Upload File!");
                    errors[file.FileName] = e.Message;
                }
            }

            var result = await _fileManager.AddFiles(filesToAdd, User.Identity.Name, s => errors[s] = string.Format(BLRes.Api_FilesController_FileNotAddable, s));

            if (!result.Ok)
            {
                return(BadRequest(result.Operation));
            }

            string message = string.Format(BLRes.Api_FilesController_SucessMessage, filesToAdd.Count, size);

            return(new UploadResult(errors, message, result.Operation));
        }