2011-06-28 54 views

回答

0
this.dialog = new SaveFileDialog(); 

      try 
      { 
       this.dialog.DefaultExt = ".txt"; 
       this.dialog.Filter = "Text Files|*.txt|Log Files|*.log|All Files|*.*"; 
       this.dialog.FilterIndex = 2; 
      } 
      catch (Exception ex) 
      { 
       this.tblError.Text = "Error configuring SaveFileDialog: " + ex.Message; 
      } 
+0

這只是設置過濾器。我希望託管應用的瀏覽器知道什麼內容類型。 – MrW

1

你在找什麼是上傳文件的MIME類型。我認爲這將在Silverlight中工作,我不明白爲什麼不,但我從來沒有在這方面使用它。您需要添加以下內容:

using System.Reflection; 
using System.Runtime.InteropServices; 

[System.Runtime.InteropServices.DllImport(@"urlmon.dll", CharSet = CharSet.Auto)] 
private extern static System.UInt32 FindMimeFromData(
    System.UInt32 pBC, 
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzUrl, 
    [MarshalAs(UnmanagedType.LPArray)] byte[] pBuffer, 
    System.UInt32 cbSize, 
    [MarshalAs(UnmanagedType.LPStr)] System.String pwzMimeProposed, 
    System.UInt32 dwMimeFlags, 
    out System.UInt32 ppwzMimeOut, 
    System.UInt32 dwReserverd 
); 

public void SomeMethod(string fileName) 
{ 
    string mimeType = getMimeFromFile(fileName); 

    if (mimeType == "text/plain") 
{ 
     // do something 
} 
} 

private string getMimeFromFile(string filename) 
    { 
     if (!File.Exists(filename)) 
      throw new FileNotFoundException(filename + " not found"); 

     byte[] buffer = new byte[256]; 
     using (FileStream fs = new FileStream(filename, FileMode.Open)) 
     { 
      if (fs.Length >= 256) 
       fs.Read(buffer, 0, 256); 
      else 
       fs.Read(buffer, 0, (int)fs.Length); 
     } 
     try 
     { 
      System.UInt32 mimetype; 
      FindMimeFromData(0, null, buffer, 256, null, 0, out mimetype, 0); 
      System.IntPtr mimeTypePtr = new IntPtr(mimetype); 
      string mime = Marshal.PtrToStringUni(mimeTypePtr); 
      Marshal.FreeCoTaskMem(mimeTypePtr); 
      return mime; 
     } 
     catch (Exception ex) 
     { 
      return ex.Message; 
     } 
    }