public void Add_2() { var m = new MultipartFormDataContent ("b"); var other = new MultipartFormDataContent ("44"); m.Add (other, "name", "fname"); Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#1"); Assert.AreEqual (165, m.Headers.ContentLength, "#2"); Assert.AreEqual ("--b\r\nContent-Type: multipart/form-data; boundary=\"44\"\r\nContent-Disposition: form-data; name=name; filename=fname; filename*=utf-8''fname\r\n\r\n--44\r\n\r\n--44--\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3"); Assert.AreEqual (other, m.First (), "#4"); Assert.IsNull (m.Headers.ContentDisposition, "#5"); Assert.AreEqual ("form-data; name=name; filename=fname; filename*=utf-8''fname", other.Headers.ContentDisposition.ToString (), "#6"); }
public void Add() { var m = new MultipartFormDataContent ("b"); var other = new MultipartFormDataContent ("44"); other.Headers.Expires = new DateTimeOffset (2020, 11, 30, 19, 55, 22, TimeSpan.Zero); m.Add (other); Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#1"); Assert.AreEqual (154, m.Headers.ContentLength, "#2"); Assert.AreEqual ("--b\r\nContent-Type: multipart/form-data; boundary=\"44\"\r\nExpires: Mon, 30 Nov 2020 19:55:22 GMT\r\nContent-Disposition: form-data\r\n\r\n--44\r\n\r\n--44--\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#3"); Assert.AreEqual (other, m.First (), "#4"); Assert.IsNull (m.Headers.ContentDisposition, "#5"); Assert.AreEqual ("form-data", other.Headers.ContentDisposition.ToString (), "#6"); }
public void Ctor () { using (var m = new MultipartFormDataContent ("b")) { m.Headers.Add ("extra", "value"); Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#1"); Assert.IsNull (m.Headers.ContentDisposition, "#2"); Assert.AreEqual (14, m.Headers.ContentLength, "#3"); Assert.AreEqual ("--b\r\n\r\n--b--\r\n", m.ReadAsStringAsync ().Result, "#4"); } using (var m = new MultipartFormDataContent ()) { Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#11"); Assert.AreEqual (84, m.Headers.ContentLength, "#12"); } using (var m = new MultipartFormDataContent ("ggg")) { Assert.AreEqual ("multipart/form-data", m.Headers.ContentType.MediaType, "#21"); Assert.AreEqual (18, m.Headers.ContentLength, "#22"); } }
/** * Converts the Report to the Web Server's desired format, JSON. * It then posts the JSON file to the upload address above. **/ public async Task<Boolean> UploadToServer() { HttpClient client = new HttpClient(); client.BaseAddress = uploadAddress; // In here, a MIME container is used to initialize the JSON form. MultipartFormDataContent form = new MultipartFormDataContent(); //create the string for description, tags, and geolocation HttpContent content = new StringContent(ConvertReportInformationForUpload()); form.Add(content, "\"data\""); //convert image into byte array so we can create a memorystream HttpContent image = new StreamContent(new MemoryStream(await convertImageToByte())); image.Headers.ContentDisposition = new ContentDispositionHeaderValue("form-data") { Name = "\"image\"", FileName = "\"" + pollutionImage.Name + "\"" }; image.Headers.ContentType = new MediaTypeHeaderValue("image/jpeg"); form.Add(image); Debug.WriteLine(client.DefaultRequestHeaders.ToString()); Debug.WriteLine(form.Headers); Debug.WriteLine((await form.ReadAsStringAsync()) + "\n"); //try and post to server var response = await client.PostAsync(uploadAddress, form); Debug.WriteLine(response); return response.IsSuccessStatusCode; }