Created
August 31, 2021 14:31
-
-
Save erdtsieck/bee0c54d1f5b377b8b0d73fe19637871 to your computer and use it in GitHub Desktop.
multipartformClient c#
This file contains hidden or 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
using System; | |
using System.IO; | |
using System.Net; | |
using System.Net.Http; | |
using System.Net.Http.Headers; | |
string path = @"c:\temp\MyTest.txt"; | |
await using FileStream fileStream = File.OpenRead(path); | |
var httpClient = new HttpClient(); | |
var formData = new MultipartFormDataContent(); | |
using var streamContent = new StreamContent(fileStream); | |
using var fileContent = new ByteArrayContent(await streamContent.ReadAsByteArrayAsync()); | |
fileContent.Headers.ContentType = MediaTypeHeaderValue.Parse("multipart/form-data"); | |
formData.Add(fileContent, "file", Path.GetFileName(path)); | |
var response = await httpClient.PostAsync("https://localhost:5001/FileUpload/UploadLargeFile", formData); | |
if (response.IsSuccessStatusCode) | |
{ | |
Console.WriteLine("Succes"); | |
} | |
else | |
{ | |
Console.WriteLine("No Succes"); | |
throw new WebException($"The remote server returned unexpected status code: {response.StatusCode} - {response.ReasonPhrase}."); | |
} | |
Console.ReadLine(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment