Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using(var client = new HttpClient())
- using(var content = new MultipartFormDataContent())
- {
- client.BaseAddress = new Uri("http://localhost:8080/");
- var fileContent = new ByteArrayContent(File.ReadAllBytes(fileName));
- fileContent.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment")
- {
- FileName = "foo.jpg"
- };
- content.Add(fileContent);
- FeedItemParams parameters = new FeedItemParams()
- {
- Id = "1234",
- comment = "Some comment about this or that."
- };
- content.Add(new ObjectContent<FeedItemParams>(parameters, new JsonMediaTypeFormatter()), "parameters");
- content.Headers.ContentType = new System.Net.Http.Headers.MediaTypeHeaderValue("multipart/form-data");
- var result = client.PostAsync("/api/ImageServices", content).Result;
- public async Task<HttpResponseMessage> Post([FromBody]FeedItemParams parameters)
- using (var client = new HttpClient())
- {
- using (var content = new MultipartFormDataContent())
- {
- client.BaseAddress = new Uri("http://localhost:54711/");
- content.Add(new StreamContent(File.OpenRead(@"d:foo.jpg")), "foo", "foo.jpg");
- var parameters = new FeedItemParams()
- {
- Id = "1234",
- Comment = "Some comment about this or that."
- };
- content.Add(new ObjectContent<FeedItemParams>(parameters, new JsonMediaTypeFormatter()), "parameters");
- var result = client.PostAsync("/api/Values", content).Result;
- }
- }
- public async Task<HttpResponseMessage> PostFormData()
- {
- // Check if the request contains multipart/form-data.
- if (!Request.Content.IsMimeMultipartContent())
- {
- throw new HttpResponseException(HttpStatusCode.UnsupportedMediaType);
- }
- string root = HttpContext.Current.Server.MapPath("~/App_Data");
- var provider = new MultipartFormDataStreamProvider(root);
- // Read the form data.
- await Request.Content.ReadAsMultipartAsync(provider);
- //use provider.FileData to get the file
- //use provider.FormData to get FeedItemParams. you have to deserialize the JSON yourself
- return Request.CreateResponse(HttpStatusCode.OK);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement