Hi @Mohan,Suresh
This issue typically occurs because of how Azure App Service handles long-running requests. The default timeout for HTTP requests in Azure App Service is 230 seconds (or about 4 minutes). To stream files properly, you might need to make a few adjustments.
Here are some suggestions to resolve this issue:
- Increase the Timeout Limit
<configuration>
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="2147483648" />
</requestFiltering>
</security>
<httpRuntime targetFramework="4.6.1" executionTimeout="3600" maxRequestLength="2147483648" />
</system.webServer>
</configuration>
- Implement Chunked Transfer Encoding
Chunked transfer encoding allows data to be sent in chunks, which is useful for streaming large files. Here’s an example of how you can implement it in your API:
[Route("download-document")]
[HttpPost]
public async Task GetDocumentFromBlob([FromBody] string filePath)
{
Response.ContentType = "application/octet-stream";
var (stream, contentType) = await this.docService.GetBlobStreamAsync(filePath);
// Use a buffer to read chunks of the file
byte[] buffer = new byte[64 * 1024];
int bytesRead;
while ((bytesRead = await stream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await Response.Body.WriteAsync(buffer, 0, bytesRead);
await Response.Body.FlushAsync();
}
}
- Use Azure Blob Storage Directly
Instead of streaming the file through your API, you can generate a SAS (Shared Access Signature) token for the blob and return the URL to the client. This way, the client can download the file directly from Azure Blob Storage.
[Route("download-document-url")]
[HttpPost]
public async Task<IActionResult> GetDocumentUrl([FromBody] string filePath)
{
var blobClient = new BlobClient(connectionString, containerName, filePath);
var sasToken = blobClient.GenerateSasUri(BlobSasPermissions.Read, DateTimeOffset.UtcNow.AddHours(1));
return Ok(new { Url = sasToken.ToString() });
}
I hope one of these solutions can help you resolve the issue.