public async Task <JsonResult> UploadFile(IEricaChats_FileDTO ericaChats_FileDTO)
        {
            try
            {
                var result = await _ericaChatsFilesRepository.UploadFileFromBytesAsync(ericaChats_FileDTO.FileNameGuid, Convert.FromBase64String(ericaChats_FileDTO.FileBytesAsAsBase64String));

                return(new JsonResult(result, new JsonSerializerSettings()
                {
                    TypeNameHandling = TypeNameHandling.Auto
                }));
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message, ex);
            }
        }
Пример #2
0
        public void TestUploadAndDownloadFile()
        {
            IEricaChats_FileDTO ericaChats_FileDTO = new EricaChats_FileDTO();

            //************************ Test Upload
            //Set up the request File DTO and it's contents
            byte[] fileBytes = GetEmbeddedResourceAsBytes("TestFiles/TextFile.txt");
            string fileBytesAsBase64String = GetBase64StringFromBytes(fileBytes);

            ericaChats_FileDTO.FileBytesAsAsBase64String = fileBytesAsBase64String;
            ericaChats_FileDTO.FileNameGuid = Guid.NewGuid().ToString();
            string payload = JsonMarshaller.Marshall(ericaChats_FileDTO);

            //Call the File Manager to perform an upload
            HttpResponseMessage uploadResponse = GetFileManagerResponse(payload, "UploadFile", true);

            Assert.IsNotNull(uploadResponse);
            Assert.IsTrue(uploadResponse.IsSuccessStatusCode);


            //************************ Test Download
            //Now use the File Guid to Download the file using the File Manager
            string endpoint = $"DownloadFile/{ericaChats_FileDTO.FileNameGuid}";
            HttpResponseMessage response = GetFileManagerResponse(string.Empty, endpoint);

            Assert.IsNotNull(response);
            Assert.IsTrue(response.IsSuccessStatusCode);

            //Get the content body from the response
            string contentBody = GetResponseContentBody(response);

            Assert.IsFalse(String.IsNullOrEmpty(contentBody));

            //Compare the response File DTO contents to the request File DTO contents. They should be identical.
            IEricaChats_FileDTO ericaChats_FileDTO_Downloaded = JsonMarshaller.UnMarshall <EricaChats_FileDTO>(contentBody);

            Assert.IsTrue(String.Compare(ericaChats_FileDTO.FileNameGuid, ericaChats_FileDTO_Downloaded.FileNameGuid, StringComparison.CurrentCulture) == 0);
            Assert.IsTrue(String.Compare(ericaChats_FileDTO.FileBytesAsAsBase64String, ericaChats_FileDTO_Downloaded.FileBytesAsAsBase64String, StringComparison.CurrentCulture) == 0);
        }