一:能否在winform下获得文件的MimeType,如果能,怎么获取?
二:能否在webform下获得数据库中某文件的MimeType,如果能,怎么获取?(该文件在sqlserver2000下以binary保存)
三:webform下,假如现在用某种方法打开一文件放在内存中(filestream,stream或者用htmlinputfile等打开),能否获得该文件的MimeType(用htmlinputfile打开的已知,file1.posetfile.contenttype)?

解决方案 »

  1.   

    MimeType不知道是啥东东!呵呵,!
      

  2.   

    MimeType
    我也不知道是什么东西,不会就是说的是文件的类型吧?
      

  3.   

    <%@ Import Namespace="System.IO" %>
    <%@ page Language="C#" debug="true" %>
    <html>
    <head>
    <title>上传文件 , http://www.chinabs.net </title>
    <script language="C#" runat="server">
     //This method is called when the "upload" button id pressed
     public void UploadFile(object sender , EventArgs E)
     {
       //检查上传文件不为空
       if(myFile.PostedFile!=null)
       {     
      string nam = myFile.PostedFile.FileName ;
      //取得文件名(抱括路径)里最后一个"."的索引
      int i= nam.LastIndexOf(".");
      //取得文件扩展名
      string newext =nam.Substring(i);
      //这里我自动根据日期和文件大小不同为文件命名,确保文件名不重复
      DateTime now = DateTime.Now; 
      string newname=now.DayOfYear.ToString()+myFile.PostedFile.ContentLength.ToString(); 
      //保存文件到你所要的目录,这里是IIS根目录下的upload目录.你可以改变.
      //注意: 我这里用Server.MapPath()取当前文件的绝对目录.在asp.net里"\"必须用"\\"代替
      myFile.PostedFile.SaveAs(Server.MapPath("\\upload\\"+newname+newext)); 
      //得到这个文件的相关属性:文件名,文件类型,文件大小
      fname.Text=myFile.PostedFile.FileName;
      fenc.Text=myFile.PostedFile.ContentType ;
      fsize.Text=myFile.PostedFile.ContentLength.ToString();
       }
     }</script>
    </head>
    <body>
    <center>
    <h3> 文件上传的实例, 来自<a href="http://www.chinabs.net">中国BS网</a></h3>
    <form id="uploderform" method="post" action="FileUpload.aspx" enctype="multipart/form-data"  runat="server" >
    <table border="1" cellspacing="2" cellpadding="2" >
    <tr> <td><h5>选择要上传的文件:</h5></td</tr>
    <tr>
    <td>
    <input type="file" id="myFile" runat="server" NAME="myFile">
    </td>
    </tr>
    <tr><td>
    <input type="button"  value="上 传" OnServerClick="UploadFile" runat="server" ID="Button1" NAME="Button1">
    </td></tr>
    </table>
    </form>
    <br>
    <br>
    <table border="1" cellspacing="2">
    <tr><td><b>文件资料</b></td>
    <td>&nbsp;</td> 
    </tr>
    <tr>
    <td>文件名 :</td>
    <td><asp:label id="fname" text="" runat="server" /></td></tr>
    <tr>
    <td>文件类型 :</td>
    <td><asp:label id="fenc" runat="server" /></td></tr>
    <tr>
    <td>文件大小 :(in bytes)</td>
    <td><asp:label id="fsize" runat="server" /></td></tr>
    </table>
    <br>
    <br>
    <br>
    </center>
    </body>
    </html>
      

  4.   

    MIME is an abbreviation for Multipurpose Internet Mail Extension
      

  5.   

    To  goody9807():
        首先谢谢你的回答,但是现在情况是:我有些文件是通过winform写的程序写到数据库中的,我不知道在winform下用什么方法获取到file1.PostFile.ContentType所获取到的类型,例如a.doc,在winform下我只能取到“doc”(这个是我不想要的),而file1.PostFile.ContentType能获取到"application/msword"(这个是我想要的)。
        然后在webform又要打开winform传上的文件,这样,这个类型就不是我想要的那个了,所以我很郁闷。To other:
        可能我说的MimeType大家不习惯,不好意思,这个是我个人问题,一般下大家叫MIME
      

  6.   

    MIME (Multipurpose Internet Mail Extensions). An official Internet standard that specifies how messages must be formatted so that they can be exchanged between different e-mail systems. MIME is a flexible format, which extends the Simple Mail Transfer Protocol (SMTP) to permit data, such as video, sound, and binary files to be transmitted by Internet e-mail without having to be translated into ASCII format first.
      

  7.   

    现在三个问题中只要有一个能解决(当然,第三个问题答案别是file1.posetfile.contenttype)就万事大吉了,兄弟们,帮帮忙,加加油啊
      

  8.   

    webform中用Request.ContentType()就可以直接获取传入的MIMI类型,其他的都不知道。
      

  9.   

    还有没有其他方法啊?例如获取从数据库中读的文件的MIME类型怎么获取啊?
      

  10.   

    你的这个问题看来不太乐观,文件通过myFile.PostedFile.SaveAs(Server.MapPath("\\upload\\"+newname+newext)); 这种方式传输到服务器,就默认以MIME格式编码,如果想在webform中存储文件时得到contenttype,可能必须对文件进行人为的MIME编码,看来只能等待高手了
      

  11.   

    如果想在winform中存储文件时得到contenttype
      

  12.   

    这个我不是通过myFile.PostedFile.SaveAs(...)保存到服务器的,webform的就不用考虑了,能获取到我想要的信息,winform处理的文件是用filestream直接保存到sqlserver的
      

  13.   

    在winform中存储文件肯定采用filestream的方式,但是文件只有通过网络传输时采用MIME格式,直接存储到服务器,文件肯定不采用MIME格式编码,那我们怎么才能得到文件的contenttype?如果能够把文件通过转变格式,也许可以做到——
      

  14.   

    Const THUMB_WIDER As Int32 = 90 ' can be dynamic value with parameters
    Const COMPRESSION_QUALITY As Int32 = 90 ' can be dynamic 
    Public Sub SaveThumbSize(ByVal sSource As String, ByVal sDestination As String)
    imgSrc = New Bitmap(sSource)
    Dim ratio As Double
    If imgSrc.Width > imgSrc.Height Then
    ratio = THUMB_WIDER / imgSrc.Width
    imgDstW = THUMB_WIDER
    imgDstH = CInt(imgSrc.Height * ratio)
    Else
    ratio = THUMB_WIDER / imgSrc.Height
    imgDstH = THUMB_WIDER
    imgDstW = CInt(imgSrc.Width * ratio)
    End If
    imgDst = New Bitmap(imgSrc, imgDstW, imgDstH)
    SaveImage(imgDst, sDestination, COMPRESSION_QUALITY)imgDst.Dispose()
    imgSrc.Dispose()
    imgDst = Nothing
    imgSrc = Nothing
    End SubPublic Sub SaveImage(ByRef img As Image, ByVal dest As String, ByVal compression As Integer)
    Dim EncoderParams As New EncoderParameters(1)
    Try
    EncoderParams.Param(0) = New EncoderParameter(Encoder.Quality, compression)
    img.Save(dest, GetEncoderInfo, EncoderParams)
    Catch ex As Exception
    'img.Save(dest)
    End Try
    EncoderParams.Dispose()
    EncoderParams = Nothing
    End SubPrivate Function GetEncoderInfo() As ImageCodecInfo
    Dim encoderReturn As ImageCodecInfo = Nothing
    Dim encoders As ImageCodecInfo() = ImageCodecInfo.GetImageEncoders
    Dim encoder As ImageCodecInfo
    For Each encoder In encoders
    If encoder.MimeType = "image/jpeg" Then
    encoderReturn = encoder
    End If
    Next
    encoders = Nothing
    Return encoderReturn
    End Function这个可能对你有些提示
      

  15.   

    winform下可以先获取文件扩展名,然后去注册表中查询    Private Const DEFAULT_MIME As String = "application/octetstream"
        Private Function getMIME(ByVal Extension As String) As String
            Dim regKey As Microsoft.Win32.RegistryKey
            regKey = Microsoft.Win32.Registry.ClassesRoot.OpenSubKey(Extension)
            If regKey Is Nothing Then
                getMIME = DEFAULT_MIME
            Else
                getMIME = regKey.GetValue("Content Type", DEFAULT_MIME)
            End If
            regKey = Nothing
        End Function    '调用
        MsgBox(getMIME(".doc"))