Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public async Task<bool> addImages(Project project)
- {
- bool success = false;
- if (project.uuid.Equals(""))
- {
- return false;
- }
- mUploadedImages = 0;
- int totalImages = project.NumImages;
- mProject = project;
- string service = "service/imagen-project/add";
- string serviceURL = mBaseURL + "/pwpcloud/" + service;
- string boundary = "---------------------------" + DateTime.Now.Ticks.ToString("x");
- for (int i = 1; i <= totalImages; i++)
- {
- //Read the image and put it inside a byte array
- string filePath = project.Name + "_ID" + project.ID + "_Pictures/Pic_"+i+".jpg";
- string fileUrl = "isostore:/" + filePath;
- IsolatedStorageFile isoStore = IsolatedStorageFile.GetUserStoreForApplication();
- //Rest request
- HttpClient restClient = new HttpClient();
- restClient.BaseAddress = new Uri(mBaseURL);
- restClient.DefaultRequestHeaders.TryAddWithoutValidation("Content-Type", "multipart/form-data; boundary=" + boundary);
- // autentication
- setAuthorization(restClient, service,"POST");
- // This is the postdata
- MultipartFormDataContent content = new MultipartFormDataContent(boundary);
- StringContent textPart = new StringContent(project.uuid, Encoding.UTF8);
- content.Add(textPart, "project");
- var fileStream = isoStore.OpenFile(filePath, FileMode.Open, FileAccess.Read);
- fileStream.Position = 0;
- StreamContent imagePart = new StreamContent(fileStream);
- imagePart.Headers.Add("Content-Type", "image/jpeg");
- content.Add(imagePart, "file", project.uuid + "_Image_" + i + ".jpg");
- HttpRequestMessage req = new HttpRequestMessage(HttpMethod.Post, serviceURL);
- req.Content = content;
- HttpResponseMessage response = null;
- //string responseBodyAsText = "";
- try
- {
- response = await restClient.SendAsync(req);
- //responseBodyAsText = await response.Content.ReadAsStringAsync();
- if (response.StatusCode==HttpStatusCode.Conflict)
- {
- if (i == 5)
- {
- success = true;
- }
- continue;
- }else if (response.StatusCode == HttpStatusCode.Created)
- {
- //MessageBox.Show("imagen " + i + " subida.");
- if (i==5)
- {
- success = true;
- }
- }
- else
- {
- return false;
- }
- }
- catch (Exception e)
- {
- string err = e.Message;
- }
- }
- return success;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement