Pregunta

I have an ASP.NET web application hosted on a CMS(sharepoint 2013) that has built in search.

When running a file query with search, the only indication of the file type is an extension string, formatted without a period "PNG" or "JPG"

I do not get a file back, I just get metadata describing the file. To be clear, I do not get any MIME types or file objects, I JUST get an extension string.

I looked around the site for a question like this, and I don't see one.

It seems like there could be some sort of method that returns a string, and takes a string input in its signature, and searches through an object collection of some type, as follows:

FINAL RESULT:

static class FileIcons
{
    static public string PDF = string.Format("{0}", SPContext.Current.Site.ServerRelativeUrl + "_layouts/15/monkeysphere/images/icons/pdf.png");
    static public string Word = string.Format("{0}", SPContext.Current.Site.ServerRelativeUrl + "_layouts/15/monkeysphere/images/icons/word.png");
    static public string Generic = string.Format("{0}", SPContext.Current.Site.ServerRelativeUrl + "_layouts/15/monkeysphere/images/icons/file.png");

    static private Dictionary<String, String> IconUrls = new Dictionary<String,String>() 
                        {
                            {"JPG", Picture},{"JPEG", Picture},{"GIF", Picture},{"PNG", Picture},
                            {"PDF", PDF},{"DOC", Word},{"DOCX", Word},{"XLS", Excel},{"XLSX", Excel},{"PPT", PowerPoint},{"PPTX", PowerPoint}
                        };

    static public string GetIconUrlFromExtension(string extension)
    {
        var formattedExtension = extension.Trim().Replace(".","").ToUpper();
        if (string.IsNullOrEmpty(extension) || !IconUrls.ContainsKey(formattedExtension))
            return Generic;
        return IconUrls[formattedExtension];
    }
}
¿Fue útil?

Solución

I think a Dictionary would be a better option. Something like this:

public static class IconUrls {
    static Dictionary<string, string> _extensions;
    static string DefaultUrl = "~/siteroot/images/icons/genericicon.png";

    static IconUrls {
        extensions = new Dictionary<string, string>() {
            { "JPG", "~/siteroot/images/icons/jpegicon.png" }, 
            { "JPEG", "~/siteroot/images/icons/jpegicon.png" }
        }
    }

    public static Dictionary<string, string> Extensions {
        get {
            return _extensions;
        }
    }

    public static string GetIconUrl(string extension) {
    {
        if(string.IsNullOrEmpty(extension) || !_dictionary.ContainsKey(extension.Trim().ToUpper()))
            return DefaultUrl;
        return _extensions[extension.Trim().ToUpper()];
    }
}

You could also load the mappings from a configuration file so as not to hard code them.

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top