File upload in xamarin forms

Moon 1 Reputation point
2022-11-08T12:56:27.95+00:00

I want to upload image from mobile application. Here is the mobile side code

   public async Task<bool> UploadFile(string fileName,byte[] imageBytes = null)  
           {  
               bool isSuccess = false;  
               try  
               {  
                   if (imageBytes != null && imageBytes.Length > 0)  
                   {                      
                     MultipartFormDataContent imageRequest = new MultipartFormDataContent();  
                     ByteArrayContent byteArrayContent = new ByteArrayContent(imageBytes);  
                     imageRequest.Add(byteArrayContent, "Files", fileName);  
                       if (_appService.IsApiAvailable().Result)  
                       {  
                           string url = FileUploadEndpoint;  
                           isSuccess = await _requestProvider.PostAsync<MultipartFormDataContent, bool>(url, imageRequest, accessToken, null).ConfigureAwait(false);  
                       }  
                   }  
               }  
               catch (Exception ex)  
               {  
                   Support.Error.Show(ex);  
               }  
               return isSuccess;  
           }  

Here is the API side code

   public bool PostAsync([FromForm]FilesUploadRequest filesUploadRequest)  
           {  
               //some code  
           }  
     
     
    public class FilesUploadRequest  
       {  
           public List<IFormFile> Files { get; set; }  
       }  

Response from API is always coming false though it is working fine from swagger. Can anyone suggest me where I am doing wrong?

Azure Files
Azure Files
An Azure service that offers file shares in the cloud.
884 questions
Azure Blob Storage
Azure Blob Storage
An Azure service that stores unstructured data in the cloud as blobs.
1,680 questions
ASP.NET MVC
ASP.NET MVC
A Microsoft web application framework that implements the model-view-controller (MVC) design pattern.
846 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
8,164 questions
{count} votes