Anuya
Anuya

Reputation: 8350

Show icons corresponding to the file type in asp.net

I need to show the ICONS corresponding to file type. For example i do have "sample.doc" in a string, which i show it in a anchor tag as link to make it downloadable. The link will have file name as shown below

<a title="Sample" target="_blank" href="\files\sample.doc">Sample</a>

What i need is i want the link to be shown like below

enter image description here

There are many filetypes i would require to show on the web page. How can I achieve this. A starting point will be good for me to continue with it.

Upvotes: 0

Views: 2801

Answers (3)

Aman Sharma
Aman Sharma

Reputation: 21

We can show file according to file extension in asp.net. All we need to download file icon images then write some code to check extension of file and get file icon according to that.

Following function can be used to get file Icon:

private string GetIconForFile(string FileName)
    {
        string extension = Path.GetExtension(FileName);
        extension = extension.Trim('.').ToLower();
        return "~/fileicons/" + extension + ".png";
    }

Here is the link to full example:http://programmerskills.blogspot.in/2016/05/show-file-icons-with-filename-in-aspnet.html

Upvotes: 0

Ramunas
Ramunas

Reputation: 3883

I usually do this (pseudo-code)

foreach(var file in files){
 var extension = Path.GetExtension(file)

 ...
 <a title="Sample" target="_blank" href="\files\sample.doc">
   <img src="/images/@(extension).png" />
   Sample
 </a>
}

Upvotes: 1

Related Questions