I want to download the zip file without loading it into the browser. However, when I hit the endpoint, instead of triggering a download, I am getting gibberish text in response.
This is my API endpoint.
[HttpGet("downloadfile")]
public async Task<IActionResult> Downloadfile(string userId, CancellationToken ct)
{
string docId = $"tinyurl:{userId}";
DocumentResponse response1 = await blobManager.GetCustomUrlByUserId(docId, ct);
TinyUrl tinyUrl = JsonConvert.DeserializeObject<TinyUrl>(response1.Content);
var response = await wc.GetAsync(tinyUrl.ActualUrl, HttpCompletionOption.ResponseHeadersRead, ct);
if (!response.IsSuccessStatusCode)
{
return NotFound();
}
var contentStream = await response.Content.ReadAsStreamAsync();
var fileName = "file.zip";
return new FileStreamResult(contentStream, "application/octet-stream")
{
FileDownloadName = fileName
};
}
This is my react code
export const DownloadZip = async (uri) => {
const method = 'GET';
const headers = await auth.getTokenHeader(true);
headers.append('Accept', 'application/octet-stream');
return retryFetch(uri, { method, headers });
};