Advertisement
Guest User

Untitled

a guest
Oct 30th, 2014
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.63 KB | None | 0 0
  1. public async Task<bool> addImages(Project project)
  2. {
  3. bool success = false;
  4. if (project.uuid.Equals(""))
  5. {
  6. return false;
  7. }
  8.  
  9. mUploadedImages = 0;
  10. int totalImages = project.NumImages;
  11.  
  12. mProject = project;
  13. string service = "service/imagen-project/add";
  14. string serviceURL = mBaseURL + "/pwpcloud/" + service;
  15.  
  16. string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
  17.  
  18. for (int i = 1; i <= totalImages; i++)
  19. {
  20. //Read the image and put it inside a byte array
  21.  
  22. string filePath = project.Name + "_ID" + project.ID + "_Pictures/Pic_"+i+".jpg";
  23. string fileUrl = "isostore:/" + filePath;
  24. IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
  25.  
  26. //Rest request
  27. HttpClient restClient = new HttpClient();
  28. restClient.BaseAddress = new Uri(mBaseURL);
  29. restClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
  30.  
  31. // autentication
  32. setAuthorization(restClient, service,"POST");
  33.  
  34. // This is the postdata
  35. MultipartFormDataContent content = new MultipartFormDataContent(boundary);
  36. StringContent textPart = new StringContent(project.uuid, Encoding.UTF8);
  37. content.Add(textPart, "project");
  38.  
  39. var fileStream = isoStore.OpenFile(filePath, FileMode.Open, FileAccess.Read);
  40. fileStream.Position = 0;
  41.  
  42. StreamContent imagePart = new StreamContent(fileStream);
  43. imagePart.Headers.Add("Content-Type", "image/jpeg");
  44. content.Add(imagePart, "file", project.uuid + "_Image_" + i + ".jpg");
  45.  
  46.  
  47. HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, serviceURL);
  48. req.Content = content;
  49. HttpResponseMessage response = null;
  50. //string responseBodyAsText = "";
  51.  
  52. try
  53. {
  54. response = await restClient.SendAsync(req);
  55. //responseBodyAsText = await response.Content.ReadAsStringAsync();
  56. if (response.StatusCode==HttpStatusCode.Conflict)
  57. {
  58. if (i == 5)
  59. {
  60. success = true;
  61. }
  62. continue;
  63. }else if (response.StatusCode == HttpStatusCode.Created)
  64. {
  65. //MessageBox.Show("imagen " + i + " subida.");
  66. if (i==5)
  67. {
  68. success = true;
  69. }
  70. }
  71. else
  72. {
  73. return false;
  74. }
  75. }
  76. catch (Exception e)
  77. {
  78. string err = e.Message;
  79. }
  80. }
  81. return success;
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement