• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 709
  • Last Modified:

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

0
jabcoco
Asked:
jabcoco
  • 2
1 Solution
 
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
 
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

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now