Пример #1
0
        public async Task <AttachmentMold> Post(SetAttachment request)
        {
            var session = Request.ThrowIfUnauthorized();
            var file    = Request.Files.FirstOrDefault();

            if (file == null)
            {
                throw HttpError.BadRequest("Error: No file to download");
            }

            var stream = file?.InputStream;
            var fileId = $"{pref}{Guid.NewGuid()}";

            var fileUpload = new Attachment()
            {
                Id             = fileId,
                FileName       = file?.FileName,
                SenderId       = session.UserAuthId,
                Hash           = SaveFile(fileId.Replace(pref, string.Empty), stream),
                UploadDateTime = DateTimeOffset.Now,
                Size           = stream.Length
            };

            await RavenSession.StoreAsync(fileUpload);

            await RavenSession.SaveChangesAsync();

            var mapped = Mapper.Map <AttachmentMold>(fileUpload);

            return(mapped);
        }
        private List <AttachmentMold> UploadAttachment()
        {
            var response = new SetAttachment();
            var result   = new List <AttachmentMold>();

            Task.WaitAll(
                AttachmentsPath.Select(attachment => Task.Run(() =>
            {
                var reqestResult =
                    _serviceClient
                    .PostFileWithRequest <AttachmentMold>(File.OpenRead(attachment), new FileInfo(attachment).Name, response);

                result.Add(reqestResult);
            })).ToArray());

            return(result);
        }