Send image to Server from Xamarin Forms

Vasanthakumar M 251 Reputation points
2021-03-19T07:05:38.76+00:00

Dear Techie,

I'm new to Xamarin forums, How to send an image to server in Xamarin forums . I tried out but i could not able to solve this .

Thanks,
Vasanth

Developer technologies | .NET | Xamarin
SQL Server | Other
0 comments No comments
{count} votes

Answer accepted by question author
  1. Anonymous
    2021-03-19T12:29:36.883+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    If you want to upload a file from Xamarin Forms app to ASP.NET web application.Here is a simple code.

       private MediaFile _mediaFile;  
         
           private async void UploadFile_Clicked(object sender, EventArgs e)  
           {  
               var content = new MultipartFormDataContent();  
         
               content.Add(new StreamContent(_mediaFile.GetStream()),  
                   "\"file\"",  
                   $"\"{_mediaFile.Path}\"");  
         
               var httpClient = new HttpClient();  
         
               var uploadServiceBaseAddress = "http://uploadtoserver.azurewebsites.net/api/Files/Upload";  
               //"http://localhost:12214/api/Files/Upload";  
         
               var httpResponseMessage = await httpClient.PostAsync(uploadServiceBaseAddress, content);  
         
               RemotePathLabel.Text = await httpResponseMessage.Content.ReadAsStringAsync();  
           }  
    

    And shows to get and save that file with an ASP.NET application:

       public class UploadsController : ApiController  
       {  
           [Route("api/Files/Upload")]  
           public async Task<string> Post()  
           {  
               try  
               {  
                   var httpRequest = HttpContext.Current.Request;  
         
                   if (httpRequest.Files.Count > 0)  
                   {  
                       foreach (string file in httpRequest.Files)  
                       {  
                           var postedFile = httpRequest.Files[file];  
         
                           var fileName = postedFile.FileName.Split('\\').LastOrDefault().Split('/').LastOrDefault();  
         
                           var filePath = HttpContext.Current.Server.MapPath("~/Uploads/" + fileName);  
         
                           postedFile.SaveAs(filePath);  
         
                           return "/Uploads/" + fileName;  
                       }  
                   }  
               }  
               catch (Exception exception)  
               {  
                   return exception.Message;  
               }  
         
               return "no files";  
           }  
       }  
    

    Best Regards,

    Leon Lu


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.