Advertisement
Guest User

Untitled

a guest
May 30th, 2016
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. using(var client = new HttpClient())
  2. using(var content = new MultipartFormDataContent())
  3. {
  4. client.BaseAddress = new Uri("http://localhost:8080/");
  5. var fileContent = new ByteArrayContent(File.ReadAllBytes(fileName));
  6. fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
  7. {
  8. FileName = "foo.jpg"
  9. };
  10.  
  11. content.Add(fileContent);
  12. FeedItemParams parameters = new FeedItemParams()
  13. {
  14. Id = "1234",
  15. comment = "Some comment about this or that."
  16. };
  17. content.Add(new ObjectContent<FeedItemParams>(parameters, new JsonMediaTypeFormatter()), "parameters");
  18. content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
  19.  
  20. var result = client.PostAsync("/api/ImageServices", content).Result;
  21.  
  22. public async Task<HttpResponseMessage> Post([FromBody]FeedItemParams parameters)
  23.  
  24. using (var client = new HttpClient())
  25. {
  26. using (var content = new MultipartFormDataContent())
  27. {
  28. client.BaseAddress = new Uri("http://localhost:54711/");
  29.  
  30. content.Add(new StreamContent(File.OpenRead(@"d:foo.jpg")), "foo", "foo.jpg");
  31.  
  32. var parameters = new FeedItemParams()
  33. {
  34. Id = "1234",
  35. Comment = "Some comment about this or that."
  36. };
  37. content.Add(new ObjectContent<FeedItemParams>(parameters, new JsonMediaTypeFormatter()), "parameters");
  38.  
  39. var result = client.PostAsync("/api/Values", content).Result;
  40. }
  41. }
  42.  
  43. public async Task<HttpResponseMessage> PostFormData()
  44. {
  45. // Check if the request contains multipart/form-data.
  46. if (!Request.Content.IsMimeMultipartContent())
  47. {
  48. throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
  49. }
  50.  
  51. string root = HttpContext.Current.Server.MapPath("~/App_Data");
  52. var provider = new MultipartFormDataStreamProvider(root);
  53.  
  54. // Read the form data.
  55. await Request.Content.ReadAsMultipartAsync(provider);
  56.  
  57. //use provider.FileData to get the file
  58. //use provider.FormData to get FeedItemParams. you have to deserialize the JSON yourself
  59.  
  60. return Request.CreateResponse(HttpStatusCode.OK);
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement