Reputation: 61
In my code I'm doing a call to an api that should answer me with a file (excel), I want to save this file in my pc, I tryed many solutions provided here but nothing seems to work for me
HttpResponseMessage response = client.PostAsync(url, stringContent).Result;
if (response.IsSuccessStatusCode)
{
string results = response.Content.ReadAsStringAsync().Result;
byte[] toBeReturned = JsonConvert.DeserializeObject<byte[]>(results);
log.Info("downloadMasterList FINISH");
return File(toBeReturned, "application/octet-stream");
}
Upvotes: 1
Views: 940
Reputation: 6103
After you get the response (assuming you are getting byte[]
), you need to save it to a local drive. You can use File.WriteAllBytes to save.
var saveDir = Path.Combine(Path.GetTempPath(), "some_excel.xlsx");
using (var client = new HttpClient())
{
var response = client.PostAsync(url, content).Result;
if (response.IsSuccessStatusCode)
{
var content = response.Content.ReadAsByteArrayAsync().Result;
File.WriteAllBytes(saveDir, content);
}
}
Upvotes: 1