private async Task UploadFileToFileManager(IEricaChats_MessageDTO ericaMessageRequest) { IEricaChats_FileDTO ericaChats_FileDTO = new EricaChats_FileDTO(); ericaChats_FileDTO.FileNameGuid = ericaMessageRequest.FileAttachmentGUID; ericaChats_FileDTO.FileBytesAsAsBase64String = ericaMessageRequest.FileBytesAsAsBase64String; string ericaChats_FileDTO_Json = JsonMarshaller.Marshall(ericaChats_FileDTO); var client = _httpClientFactory.CreateClient(); var content = new StringContent(ericaChats_FileDTO_Json, Encoding.UTF8, "application/json"); HttpResponseMessage response = await client.PostAsync("http://localhost:50002/api/EricaChatsFiles/UploadFile", content); response.EnsureSuccessStatusCode(); }
public async Task <JsonResult> DownloadFile(string fileNameGuid) { try { IEricaChats_FileDTO result = new EricaChats_FileDTO(); var fileBytes = await _ericaChatsFilesRepository.DownloadFileAsBytesAsync(fileNameGuid); result.FileNameGuid = fileNameGuid; result.FileBytesAsAsBase64String = Convert.ToBase64String(fileBytes); return(new JsonResult(result, new JsonSerializerSettings() { TypeNameHandling = TypeNameHandling.Auto })); } catch (Exception ex) { throw new ApplicationException(ex.Message, ex); } }
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); }