Obtaining file "Content Type" before uploading.

Normally IE (or other browser) will pass throw header the content type for a uploaded file.
Unfortunetly this seems impossible with "Silverlight".

We have a simple upload where the user can select a file from file system and then upload to the server.
We need to pass the "content type" to the server because the server may not know the file extension and content type.

Here is a simple way to obtain contenttype from file extention, but isn't available on silverlight.
Even if it's possible it is not viable because of security and access purpose of different clients desktop.

Any idea on how i can retreive "content type" of uploading file in a silverlight application?

thanks
private string GetContentType(string fileName) 
{ 
    string contentType = "application/octetstream"; 
    string ext = System.IO.Path.GetExtension(fileName).ToLower(); 
    Microsoft.Win32.RegistryKey registryKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(ext); 
    if (registryKey != null && registryKey.GetValue("Content Type") != null) 
        contentType = registryKey.GetValue("Content Type").ToString(); 
    return contentType; 
}

Open in new window

LVL 10
jabcocoAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

feddasCommented:
Examining the clients registry is something Silverlight will never allow.  Your best option would be to send what you have, the file extension, to the server.

If the server is unfamiliar with an extension you could either manually make it familiar with more extensions or query some other data source that maps file extension to Content Type.
private string GetExtension()
{
  OpenFileDialog newFile = new OpenFileDialog();

  if (newFile.ShowDialog() == true)
    return newFile.File.Extension;
  else return string.Empty;
}

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jabcocoAuthor Commented:
their is no option to retreive content type from client.
So we do it on server side base on extention and extension/mime type manual match.

thanks
0
jabcocoAuthor Commented:
thanks for the tips
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.