Search code examples
asp.net-mvcazurehrefazure-storage

A property has a space and Href is ignorning everything after it


I can download an uploaded file to Azure using a direct link, seemed the quickest and easiest way to do it. By no means the safest or the smartest. However, when I link to it in the Href, it ignores all spaces, meaning that if a user uploads a document with a space in the name, It doesn't search properly. How would I modify my code to replace any Spaces with % instead when searching?

Here is my code; The link clicked to download the file;

  <a href=@ViewBag.LinkToDownload@item.DocumentId@item.RevisionId@item.Attachment>Download</a>

The controller

 [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Download(string id)
    {
        string path = @"https://filestorageideagen.blob.core.windows.net/documentuploader/";

        return View(path + id);
    }

Solution

  • According to your description, I have tested this issue. Here are the details, you could refer to them:

    Test image: https://brucechen.blob.core.windows.net/images/hello world.jpg

    HttpUtility.UrlEncode or UrlHelper.Encode

    This method would encode space to plus (+), when encoding your blob (file) name, you could get https://brucechen.blob.core.windows.net/images/hello+world.jpg, but at this point, the file could not be accessed via your browser.

    Uri.EscapeUriString

    This is an equivalent to JavaScript's encodeURIComponent() which could encode space to %20, when using this method you could get https://brucechen.blob.core.windows.net/images/hello%20world.jpg.

    Based on your scenario, you could leverage Uri.EscapeUriString to encode your url string. Additionally, %20 is used before the ? while + after, for more details, you could refer to this issue.