Exemplo n.º 1
0
        public async Task <DocViewModel> Upload(UploadRequest request)
        {
            var fileData = request.File;
            var document = new Models.Doc
            {
                ContentType      = request.File.ContentType,
                Description      = request.Details,
                OriginalFileName = request.File.FileName,
                FileName         = request.Name,
                SizeInMbs        = request.File.Length,
                CreatedBy        = _httpContextAccessor.GetUser().userId,
                IsPrimary        = request.IsPrimary
            };
            //Save record to db
            var resp = await _repository.CreateAsync(document);

            //create server filename
            var extension = Path.GetExtension(request.File.FileName);
            var filePath  = Path.Combine(DocStore, $"{resp.Id}.{extension}");

            using (var stream = new FileStream(filePath, FileMode.Create))
            {
                await fileData.CopyToAsync(stream);
            }

            return(_mapper.Map <DocViewModel>(resp));
        }