Last active
October 1, 2023 13:08
-
-
Save linhdh/23be21d926231b7a18ccb68c6be77313 to your computer and use it in GitHub Desktop.
[C#] MultiPartFormDataContent, Upload multi files to server at a time.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
System.Net.Http.HttpRequestMessage request = new System.Net.Http.HttpRequestMessage(System.Net.Http.HttpMethod.Post, _serverUri); | |
MultipartFormDataContent form = new MultipartFormDataContent(); | |
request.Content = form; | |
HttpContent content1 = new ByteArrayContent(filePart1); | |
content1.Headers.ContentDisposition = new ContentDispositionHeaderValue("filePart1"); | |
content1.Headers.ContentDisposition.FileName = "filePart1"; | |
form.Add(content1); | |
HttpContent content2 = new ByteArrayContent(filePart2); | |
content2.Headers.ContentDisposition = new ContentDispositionHeaderValue("filePart2"); | |
content2.Headers.ContentDisposition.FileName = "filePart2"; | |
form.Add(content2); | |
_httpClient.SendAsync(request).ContinueWith((response) => | |
{ | |
ProcessResponse(response.Result); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private static void SendFileToServer(string fileFullPath) | |
{ | |
FileInfo fi = new FileInfo(fileFullPath); | |
string fileName = fi.Name; | |
byte[] fileContents = File.ReadAllBytes(fi.FullName); | |
Uri webService = new Uri(@"http://avalidwebservice.com"); | |
HttpRequestMessage requestMessage = new HttpRequestMessage(HttpMethod.Post, webService); | |
requestMessage.Headers.ExpectContinue = false; | |
MultipartFormDataContent multiPartContent = new MultipartFormDataContent("----MyGreatBoundary"); | |
ByteArrayContent byteArrayContent = new ByteArrayContent(fileContents); | |
byteArrayContent.Headers.Add("Content-Type", "application/octet-stream"); | |
multiPartContent.Add(byteArrayContent, "this is the name of the content", fileName); | |
requestMessage.Content = multiPartContent; | |
HttpClient httpClient = new HttpClient(); | |
try | |
{ | |
Task<HttpResponseMessage> httpRequest = httpClient.SendAsync(requestMessage, HttpCompletionOption.ResponseContentRead, CancellationToken.None); | |
HttpResponseMessage httpResponse = httpRequest.Result; | |
HttpStatusCode statusCode = httpResponse.StatusCode; | |
HttpContent responseContent = httpResponse.Content; | |
if (responseContent != null) | |
{ | |
Task<String> stringContentsTask = responseContent.ReadAsStringAsync(); | |
String stringContents = stringContentsTask.Result; | |
} | |
} | |
catch (Exception ex) | |
{ | |
Console.WriteLine(ex.Message); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi, It was very helpful this example.
I am testing one of the REST API ( pdf file post ) in Postman client. I got the below c# code form Postman. I want to do this programmatically. Do you have any idea how do i achieve using multipart form data.
var client = new RestClient("https://sest-stg.dev03.sbc.ase.central.us.hal.net/files/v1");
var request = new RestRequest(Method.POST);
request.AddHeader("Postman-Token", "cee45074-d613-4e58-ac25");
request.AddHeader("cache-control", "no-cache");
request.AddHeader("Content-Type", "application/pdf");
request.AddHeader("X-SM-Client-Id", "94437320-3bcf-498d-915a");
request.AddHeader("Sest-Tenant-Id", "0d3ad0cd-3bb3-4fc0-bd15");
request.AddHeader("content-type", "multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW");
request.AddParameter("multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW", "------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="file"; filename="C_70_4_01302013_101.pdf"\r\nContent-Type: application/pdf\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW\r\nContent-Disposition: form-data; name="metadata"\r\n\r\n\r\n------WebKitFormBoundary7MA4YWxkTrZu0gW--", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);