Issue downloading file with + signs in their name, from Azure Blob Storage, in Azure App Service

sekou keita 20 Reputation points
2023-12-04T17:58:41.95+00:00

I am experiencing an issue downloading a file with a + sign in its name (

14595-2023-2157-1-GCSU+Tree+Campus+Plan+2022 .docx

), specifically when my code is deployed in Azure App Service. Surprisingly, the file exists in Blob Storage and can be downloaded when my web API is run locally. Attempting to download the file when my API is deployed in Azure App Service results in the error message:

The resource you are looking for has been removed, had its name changed, or is temporarily unavailable.

However, there is no issue both locally and deployed for files whose names do not contain the + sign. I tried to identify if there are any encoding or decoding issues by adding many logs into the action method, but it seems that the endpoint is not hit on deployment. Here is my action method:

  • Visual Studio: 2022, version 17.8.2
  • ASP.NET Core 6.0 Web API
// GET: /files/fileName [HttpGet("{fileName}")]
public async Task
ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,325 questions
Azure App Service
Azure App Service
Azure App Service is a service used to create and deploy scalable, mission-critical web apps.
7,245 questions
0 comments No comments
{count} votes

Accepted answer
  1. Sumarigo-MSFT 44,816 Reputation points Microsoft Employee
    2024-02-05T04:58:52.5133333+00:00

    @sekou keita I'm glad that you were able to resolve your issue and thank you for posting your solution so that others experiencing the same thing can easily reference this! Since the Microsoft Q&A community has a policy that "The question author cannot accept their own answer. They can only accept answers by others ", I'll repost your solution in case you'd like to "Accept " the answer.

    Issue: Issue downloading file with + signs in their name, from Azure Blob Storage, in Azure App Service

    Solution: I resolved the issue by modifying my file naming convention. Initially, I used the file name as a path parameter ("/baseurl/files/{filename}"). However, I addressed the problem by switching to a query parameter approach ("/baseurl/files?filename={filename}"). I hope this adjustment can also resolve any similar issues

    ---Please remember to "Accept Answer" if any answer/reply helped, so that others in the community facing similar issues can easily find the solution.

    1 person found this answer helpful.

6 additional answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 59,806 Reputation points
    2023-12-04T22:29:09.51+00:00

    in a url, the "+" is a quote for a space. you should url encode the url. that is replace "+" with "%2B" and space with "%20".

    1 person found this answer helpful.
    0 comments No comments

  2. JasonPan - MSFT 4,791 Reputation points Microsoft Vendor
    2023-12-06T08:06:57.3866667+00:00

    Hi @sekou keita,

    The reason Bruce mentioned is correct. But we can do it easily by encoding the fileDownloadName.

    public async Task<IActionResult> DownloadFile(string fileName)
    {
        try
        {
            //...
            // Using HttpUtility.UrlEncode
            Response.Headers.Add("Content-Disposition", $"inline; filename=\"{HttpUtility.UrlEncode(fileDownloadName)}\"");
            Response.Headers.Add("contentType", contentType);
            // ...
                    
        }
        catch (RequestFailedException ex)
        {
            _logger.LogError($"File {fileName} cannot be downloaded. Error: {ex.Message}");
            return BadRequest($"The file {fileName} connot be downloaded.");
        }
    }
    

    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Jason


  3. sekou keita 20 Reputation points
    2023-12-07T14:36:28.1+00:00

    Continuing my investigations about this issue, I read somewhere that the fact that the file name is used, in the endpoint, as path parameter (base_url/files/filename) vs as query parameter (base_url/files?filename=fileName) can have some impact on how + signs and spaces are encoded. I tried moving from path parameter to query parameter and it is working now, both locally and on the deployed version in Azure App Service, without using any explicit encoding in my code.

    Thanks again Bruce and JsonPan.

    0 comments No comments

  4. BRZOZOWSKI Lukasz 0 Reputation points
    2024-01-22T10:49:42.7233333+00:00

    Hello, I'm facing same issue. What is strange that it fails for both +test+ and %2Btest%2B. For just test it works nicely. It also works for some emoji like ✔️ but not for 😅. Do you have any idea what might cause that? Thanks

    0 comments No comments