❔ POST with MultipartFormDataContent; x.Add(new StreamContent(File.OpenRead(filePath)) not working

As I'm trying to send a file it says the file is empty. Even tough it exsists. Any idea how can I attach the file via different method?
var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, EndpointURL);

var body = new MultipartFormDataContent();

if (!File.Exists(filePath))
{
MobilOrderUtils.LogDebug("File does not exist:" + filePath);
return -99;
}


var test = new StreamContent(File.OpenRead(filePath));

body.Add(new StreamContent(File.OpenRead(filePath)), "file", fileName); //Does not work

body.Add(new StringContent(dealId.ToString()), "deal_id");
request.Content = body;

var response = client.SendAsync(request).Result;
//response.EnsureSuccessStatusCode();
string res = await response.Content.ReadAsStringAsync();

var client = new HttpClient();
var request = new HttpRequestMessage(HttpMethod.Post, EndpointURL);

var body = new MultipartFormDataContent();

if (!File.Exists(filePath))
{
MobilOrderUtils.LogDebug("File does not exist:" + filePath);
return -99;
}


var test = new StreamContent(File.OpenRead(filePath));

body.Add(new StreamContent(File.OpenRead(filePath)), "file", fileName); //Does not work

body.Add(new StringContent(dealId.ToString()), "deal_id");
request.Content = body;

var response = client.SendAsync(request).Result;
//response.EnsureSuccessStatusCode();
string res = await response.Content.ReadAsStringAsync();

3 Replies
Relevant
Relevant2y ago
The file exists, but it's empty, right?
HappyTeaKid
HappyTeaKidOP2y ago
The file is not empty; It has 15kb of content. xyz.xlsx file
Accord
Accord2y ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server