Accessing N/W file using .NET REST API deployed on Azure app service

Nuwan Wickramanayaka 81 Reputation points
2024-07-02T12:57:09.5133333+00:00

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");
    }
}
Azure Virtual Machines
Azure Virtual Machines
An Azure service that is used to provision Windows and Linux virtual machines.
7,871 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,755 questions
{count} votes

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.