Lakshman Pilaka
Lakshman Pilaka

Reputation: 1951

Know if a blob exists in a container from client side

I need to know if a blob exists in a container from client side (jquery / js / or any other client framework)

Scenario

I have a profile picture of a user in one of the containers of my azure blob storage. I show it on my webpage as follows

<img class="profile pic" src="https://storageacc.blob.core.windows.net/container/empid/profile/pic" />

The image source is dynamically built during runtime. But there will be situations when the user has not uploaded his profile pic, so i need to show a blank profile pic. Means if the pic is uploaded by the user I will show pic.png else blank.png.

So, when i load the page and the image source url is built, i need to know if the pic exists or not. I want this to happen at the client side. No posting ajax to c# ws to see if the pic exists.

One solution which comes to my mind is to maintain a flag in the database and set it to true when the profile pic is uploaded by the user, check that flag while building the image url and set img src accordingly.

But I need to set the flag if the photo is uploaded, unset it if its removed. To avoid this and db intervention in the first place, do i have a solution for checking the image existence on Azure blob container from client side?

Thanks in advance.

Upvotes: 0

Views: 959

Answers (1)

Lakshman Pilaka
Lakshman Pilaka

Reputation: 1951

I could solve this by using onerror attribute in my img tag.

<img src="azure_blob_url" onerror="this.src='images/blank.png'">

if azure_blob_url doesn't exist, blank.png will be shown. I can safely avoid db intervention and ajax posts to check if the image is there in the blog storage.

thanks anyways for the 'eight' people who read this post till now.

Upvotes: 1

Related Questions