Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

How to get file extension or file mime type of an file without file extension.

RaghavendraRaghavendra Member ✭✭
edited August 2019 in Xamarin.Forms

I'm able get file type when the file name as file extension say (Filename.pdf) by the help of Path.GetExtension.
But when the file doesn't have file extension say (Filename) only, then extension goes for a toss.

Specially looking for identifying the following format/extension ie. PDF,HTML,ZIP,JPG,JPEG,PNG,DOC.

Do we have any method to get mime type of a file which does not have extension in filename(.pdf).
Looking for solution in Xamarin forms or dependency services(Xamarin.Android).

Answers

  • ColeXColeX Member, Xamarin Team Xamurai

    Try https://stackoverflow.com/a/20721303/8187800.

    C# code

     public static string getFileType(string url)
            {
                string type = null;
    
                string extension = MimeTypeMap.GetFileExtensionFromUrl(url);
                if (extension != null)
                {
                    MimeTypeMap mime = MimeTypeMap.Singleton;
                    type = mime.GetMimeTypeFromExtension(extension);
                }
                return type;
            }
    
Sign In or Register to comment.