Divya Mittal

Divya Mittal

  • 1.8k
  • 35
  • 959

Sending image using xamarin media plugin to server is v.slow

Jan 20 2019 2:43 PM
Hi,
 
I am creating a mobile application to capture image and send on my c# api. The image captured is sent to server very slow and takes about 18-20 sec for 1mb image. Image from rear camera does not even go ! Please assist how what change I shall make in my app.
 
PFB the code for mobile app and the c# api:
 
Mobile app code:
  1. private async void CaptureImage_Clicked(object sender, EventArgs e)  
  2. {  
  3. await CrossMedia.Current.Initialize();  
  4. if (!CrossMedia.Current.IsTakePhotoSupported && !CrossMedia.Current.IsPickPhotoSupported)  
  5. { }  
  6. else  
  7. {  
  8. var check = CrossMedia.Current.IsTakePhotoSupported;  
  9. try {  
  10. var options = new StoreCameraMediaOptions()  
  11. {  
  12. DefaultCamera = CameraDevice.Front, SaveToAlbum = true, Directory = "com.companyname.DemoApp", Name = DateTime.Now.ToString("yyyyMMddTHHmmss") + ".jpg" };  
  13. var file = await CrossMedia.Current.TakePhotoAsync(options);  
  14. if (file == nullreturn;  
  15. MemoryStream memoryStream = new MemoryStream();  
  16. CamPic.Source = ImageSource.FromStream(() =>  
  17. {  
  18. var stream = file.GetStream();  
  19. file.GetStream().CopyTo(memoryStream); return stream;  
  20. });  
  21. PostIntegrationForData(file);  
  22. await DisplayAlert("Done""Image Posted To Server !""Ok");  
  23. file.Dispose();  
  24. CamPic.Source = null;  
  25. }  
  26. catch(Exception Ex)  
  27. {  
  28. await DisplayAlert("Error", Ex.Message, "Ok");  
  29. await DisplayAlert("Error", Ex.StackTrace, "Ok");  
  30. //} } }  
  31. private string PostIntegrationForData(MediaFile argMediaFile)//byte[] argFile)  
  32. {  
  33. string url = string.Format("http://localhost:83/api/MyController/PostFile");  
  34. HttpClient httpClient = new HttpClient();  
  35. MultipartFormDataContent form = new MultipartFormDataContent();  
  36. form.Add(new StringContent("Sample Title"), "title");  
  37. form.Add(new StreamContent(argMediaFile.GetStream()), "FileName", argMediaFile.Path);  
  38. HttpResponseMessage response = httpClient.PostAsync(url, form).Result;  
  39. using (HttpContent content = response.Content)  
  40. {  
  41. string data = response.Content.ReadAsStringAsync().Result;  
  42. return data;  
  43. }  
  44. }  
c# API code:
  1. [HttpPost] public HttpResponseMessage PostFile()  
  2. {  
  3. HttpResponseMessage result = null;  
  4. var httpRequest = HttpContext.Current.Request;  
  5. if (httpRequest.Files.Count > 0)  
  6. {  
  7. var docfiles = new List<string>();  
  8. foreach (string file in httpRequest.Files)  
  9. {  
  10. //string FileName = httpRequest["FileName"];  
  11. string FileName = String.Empty;  
  12. var postedFile = httpRequest.Files[file];  
  13. var postedFileName = postedFile.FileName;  
  14. postedFileName = Path.GetFileName(postedFileName);  
  15. var filePath = HttpContext.Current.Server.MapPath("~/" + postedFileName);  
  16. postedFile.SaveAs(filePath); docfiles.Add(filePath);  
  17. }  
  18. result = Request.CreateResponse(HttpStatusCode.Created, docfiles);  
  19. }  
  20. else  
  21. {  
  22. result = Request.CreateResponse(HttpStatusCode.BadRequest);  
  23. }  
  24. return result;  
  25. }  
Please suggest , I have been struggling with this and could not get a solution for better performance or any way out to send the image in 1 or 2 sec.
Thanks in advance !
Regards,
Divya

Answers (3)