Friday, 21 May 2021

Send file from Android Kotlin to C# using POST

We have two applications: A C# REST-API, and a Kotlin Android application, we are using Google Platform Cloud Bucket to host the images.

A picture will be uploaded on the Android application, but the C# REST-API needs to upload it to the Google Cloud Platform.

This is the working C# code to upload a file to the Google Cloud Buckets:

        [HttpPost]
        [Route("upload")]
        public IActionResult Upload()
        {
            var storageClient = StorageClient.Create(google_credentials);

            string fileToUpload ="/Users/niel/Downloads/new_cat.jpg";
            
            using (var fileStream = new FileStream(fileToUpload, FileMode.Open,
                FileAccess.Read, FileShare.Read))
            {
                storageClient.UploadObject("test_storage_fotos", "new_cat", "image/jpeg", fileStream);
            }
            Console.WriteLine("uploaded the file successfully");

            return Ok();
        }
        

Now I need to replace fileToUpload with the content from a POST-request. Is there a way to do this? Picture from Android app > C# API > Google Buckets? The link from the C# API to Google Buckets is already working.

Is there a way in Kotlin to somehow get the byte-string of an image, post it to my C# API who takes the content and puts it in a FileStream? I than can upload the FileStream using storageClient.UploadObject? Is this a possibility?

Thanks!



from Send file from Android Kotlin to C# using POST

No comments:

Post a Comment