public async Task <List <UploadFile> > UploadAttachments(
            MrrProcessAttachmentResourceParameter parameter
            )
        {
            if (parameter == null)
            {
                throw new ArgumentNullException(nameof(parameter));
            }

            var attachments = new List <MrrProcessAttachment>();

            foreach (var file in parameter.Files)
            {
                await using var target = new MemoryStream();
                await file.CopyToAsync(target).ConfigureAwait(false);

                var attachment = new MrrProcessAttachment
                {
                    MrrProcessId = parameter.MrrProcessId,
                    FileName     = file.FileName,
                    ContentType  = file.ContentType,
                    Data         = target.ToArray(),
                    Timestamp    = DateTime.Now
                };

                attachments.Add(attachment);
            }

            await _context.MrrProcessAttachments.AddRangeAsync(attachments).ConfigureAwait(false);

            await _context.SaveChangesAsync().ConfigureAwait(false);

            var uploads = await _context.MrrProcessAttachments
                          .Where(x => x.MrrProcessId == parameter.MrrProcessId)
                          .Select(
                x =>
                new UploadFile
            {
                Uid    = x.MrrProcessAttachmentId,
                Name   = x.FileName,
                Status = "Done",
                Url    =
                    @$ "{_baseServerUrl}/mrrProcess/attachment/{x.MrrProcessAttachmentId}"
            }
                )
                          .ToListAsync()
                          .ConfigureAwait(false);

            return(uploads);
        }
예제 #2
0
        public async Task <IActionResult> UploadAttachments(
            [FromForm] MrrProcessAttachmentResourceParameter parameter
            )
        {
            try
            {
                var data = await _service.UploadAttachments(parameter).ConfigureAwait(false);

                return(Ok(data));
            }
            catch (Exception e)
            {
                return(BadRequest(e.Message));
            }
        }