I am currently reaching out to a URL to grab a file, the GET request is a responseType: 'stream'
In my API, I need to read this “stream” into some kind of object that I can use later, but I need to access the files Headers, and be able to also access the full data (to write it out or save it). These are PDF files if that makes a difference, but I’m just not sure what Type I should be using for such a thing. Here’s a sample of what I have, but the headers are not correct:
private async Task<(byte[]?, HttpContentHeaders)> ReadFileData(string url)
{
byte[]? fileData = null;
HttpContentHeaders headers = null;
using (var response = await _httpClient.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
{
response.EnsureSuccessStatusCode();
using (var stream = await response.Content.ReadAsStreamAsync())
using (var memoryStream = new MemoryStream())
{
await stream.CopyToAsync(memoryStream);
fileData = memoryStream.ToArray();
ByteArrayContent byteContent = new ByteArrayContent(fileData);
MultipartFormDataContent form = new MultipartFormDataContent();
form.Add(byteContent, "files", "files");
headers = form.Headers;
}
}
return (fileData, headers);
}