Accessing N/W file using .NET REST API deployed on Azure app service
Nuwan Wickramanayaka
81
Reputation points
I am attempting to access a network file share that resides on an Azure VM. The VM has an attached disk used specifically for file sharing. I need to access this file share from a .NET REST API, which is currently deployed on an Azure App Service. (Same Azure subscription)
I have tried using both Azure app services and Azure Container web apps. For all cases it gives couldn't file file. See the code below.
[HttpGet("download")]
public IActionResult DownloadFile([FromQuery] string fileName)
{
try
{
_logger.LogInformation("Download request received for file:- {FileName}", fileName);
// Network path where files are located
string networkPath = @"\\filepath\";
// Combine network path with the file name
string filePath = Path.Combine(networkPath, fileName);
// Check if file exists
if (!System.IO.File.Exists(filePath))
{
_logger.LogWarning("File not found: {FilePath}", filePath);
return NotFound();
}
// Read the file as a byte array
byte[] fileBytes = System.IO.File.ReadAllBytes(filePath);
_logger.LogInformation("File successfully retrieved: {FilePath}", filePath);
// Return the file as a downloadable response
return File(fileBytes, "application/octet-stream", fileName);
}
catch (Exception ex)
{
_logger.LogError(ex, "Error occurred while downloading file: {FileName}", fileName);
// Log the exception (logging not shown here)
return StatusCode(500, "Internal server error");
}
}
Sign in to answer