My below API action is supposed to send a zip file (max 100mb) as the result. There are no errors thrown in the code, however the file downloaded is 0 size. I checked the content length and there is value in the content length. what could be the reason.
[HttpGet]
[Route("AsyncDownloadFile")]
public async Task<HttpResponseMessage> AsyncDownloadFile(int ModuleID, string LicenseKey, string Version)
{
try
{
ClientOutletsDAL co = new ClientOutletsDAL();
//var localFilePath = "path/zz.zip"
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
new StreamContent(new FileStream(localFilePath, FileMode.Open, FileAccess.Read));
Stream streamToCopyTo= new MemoryStream();
using (FileStream fs = new FileStream(localFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
{
await fs.CopyToAsync(streamToCopyTo);
}
response.Content = new StreamContent(streamToCopyTo);
//Set the Response Content Length.
response.Content.Headers.ContentLength = streamToCopyTo.Length;
response.Content.Headers.ContentDisposition = new System.Net.Http.Headers.ContentDispositionHeaderValue("attachment");
response.Content.Headers.ContentDisposition.FileName = Path.GetFileName(localFilePath);
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/zip");
return response;
}
catch (Exception ex)
{
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.InternalServerError, ex.Message );
return response;
}
}