Search code examples
c#.netblob.net-6.0

Downloading data from blob url and convert to base64 .Net6


I have a blob url which contains a pdf. I want to get that pdf and convert it to base64. The url is something like this: blob:https://seller.walmart.com/51339eb8-5d7b-49a5-8f7b-258d88bc15f0 and it is only accessible in the computer that you generate it.

I tried to use HttpClient to download it, but I get an error:

System.NotSupportedException: The 'blob' scheme is not supported.

which I think happens because the PDF is generated in browser, I don't know the platform gives the url.

This is the code I tried:

static async Task<string> FetchPdfAndConvertToBase64(string pdfUrl)
{
    using var httpClient = new HttpClient();
    var response = await httpClient.GetAsync(pdfUrl);
    response.EnsureSuccessStatusCode();

    byte[] blobBytes = await response.Content.ReadAsByteArrayAsync();
    string base64string = Convert.ToBase64String(blobBytes);
    return base64string;
}

Solution

  • You're getting that error from the HttpClient because the URL is "blob:https://seller.walmart.com/51339eb8-5d7b-49a5-8f7b-258d88bc15f0".

    Remove the "blob" bit and the rest should work fine.

    var pdfURL = theRawURL.Replace("blob:","");