HttpFileCollection MyFileCollection;
MyFileCollection = Request.Files;
int FileLen;
System.IO.Stream MyStream; 
HttpPostedFile MyFile; 
MyFile=MyFileCollection.Get(0);
FileLen = MyFile.ContentLength;str=MyFile.FileName.ToString().Split( new char[] {'.'});
if (str[str.Length-1].ToUpper()!="JPG" &&  str[str.Length-1].ToUpper()!="JPGE" &&  str[str.Length-1].ToUpper()!="BMP" &&  str[str.Length-1].ToUpper()!="GIF")
{
    alertmsg("选择的文件类型不正确,只支持*.jpg,*.jpge,*.bmp,*.gif格式的文件上传!");
}

解决方案 »

  1.   

    老大,能寫成  VB.net代碼麽?
      

  2.   


    HttpFileCollection MyFileCollection;
    MyFileCollection = Request.Files;
    int FileLen;
    System.IO.Stream MyStream; 
    HttpPostedFile MyFile; 
    MyFile=MyFileCollection.Get(0);
    FileLen = MyFile.ContentLength;
    //判断是否选择了附件
    if (FileLen==0)
    {
        alertmsg("请选择要上传的照片!");
    }
    else if(FileLen/1024>2000)
    {
    //判断附件是否超大
      alertmsg("选择的照片超过限定大小,只允许上传2M以下的附件!");
    }
    else
    {
        String[] str;
        str=MyFile.FileName.ToString().Split( new char[] {'.'});
       if (str[str.Length-1].ToUpper()!="JPG" &&  str[str.Length-1].ToUpper()!="JPGE" &&  str[str.Length-1].ToUpper()!="BMP" &&  str[str.Length-1].ToUpper()!="GIF")
        {
    alertmsg("选择的文件类型不正确,只支持*.jpg,*.jpge,*.bmp,*.gif格式的文件上传!");
         }
       else
        {
    int OldW,OldH;
    OldW=(int)System.Convert.ToDouble(txtw.Value.ToString().Trim());
    OldH=(int)System.Convert.ToDouble(txth.Value.ToString().Trim());
             this.lblw.Text=OldW.ToString();
    this.lblh.Text =OldH.ToString();
    MyStream = MyFile.InputStream;
    Byte[] Input=new byte[FileLen];
    String filetype; 
    filetype = MyFile.ContentType;
    MyStream.Read(Input, 0, FileLen);
    SqlConnection Conn=new SqlConnection(Application["connstr"].ToString());
    Conn.Open();
    SqlCommand comm=new SqlCommand(); comm.Connection=Conn;
    comm.CommandText = "update 员工基础信息 set  照片=@Bmp, 照片文件类型=@type,照片大小='" + FileLen + "',照片宽='" + this.lblw.Text + "',照片高='" + this.lblh.Text + "' where id=" + Request["empid"].ToString();
    comm.Parameters.Add("@Bmp", SqlDbType.Binary,FileLen).Value =Input;
    comm.Parameters.Add("@type", SqlDbType.NVarChar).Value = filetype;
           comm.ExecuteNonQuery();
          Conn.Close(); }
    }
    }
    }
    这下你满足了吧?
      

  3.   

    Dim MyFileCollection As HttpFileCollection
            MyFileCollection = Request.Files
            Dim FileLen As Integer
            Dim MyStream As System.IO.Stream
            Dim MyFile As HttpPostedFile
            MyFile = MyFileCollection.Get(0)
            FileLen = MyFile.ContentLength
            '判断是否选择了附件
            If FileLen = 0 Then            ' alertmsg("请选择要上传的照片!")        ElseIf FileLen / 1024 > 2000 Then            '判断附件是否超大
                'alertmsg("选择的照片超过限定大小,只允许上传2M以下的附件!")        Else            Dim str() As String
                str = Split(MyFile.FileName, ".")
                If UCase(str(str.Length - 1)) <> "JPG" And UCase(str(str.Length - 1)) <> "JPGE" And UCase(str(str.Length - 1)) <> "BMP" And UCase(str(str.Length - 1)) <> "GIF" Then                'alertmsg("选择的文件类型不正确,只支持*.jpg,*.jpge,*.bmp,*.gif格式的文件上传!")            Else                Dim OldW, OldH As Integer
                    OldW = txtw.Value
                    OldH = txth.Value
                    lblw.Text = OldW
                    lblh.Text = OldH
                    MyStream = MyFile.InputStream
                    Dim Input() As Byte
                    Dim filetype As String
                    filetype = MyFile.ContentType
                    MyStream.Read(Input, 0, FileLen)
                    Dim Conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(Application("connstr"))
                    Conn.Open()
                    Dim comm As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand()
                    comm.Connection = Conn
                    comm.CommandText = "update 员工基础信息 set  照片=@Bmp, 照片文件类型=@type,照片大小='" + FileLen + "',照片宽='" + lblw.Text + "',照片高='" + lblh.Text + "' where id=" + Request("empid").ToString()
                    comm.Parameters.Add("@Bmp", SqlDbType.Binary, FileLen).Value = Input
                    comm.Parameters.Add("@type", SqlDbType.NVarChar).Value = filetype
                    comm.ExecuteNonQuery()
                    Conn.Close()
                End If
            End If
      

  4.   

    搞定了,感謝各位!!
    下面是我寫的代碼,供大家參考:        If Page.IsPostBack Then
                Dim MyFileCollection As HttpFileCollection
                MyFileCollection = Request.Files
                Dim FileLen As Integer
                Dim MyStream As System.IO.Stream
                Dim MyFile As HttpPostedFile
                MyFile = MyFileCollection.Get(0)
                FileLen = MyFile.ContentLength
                If FileLen = 0 Then
                    WinFormsToWeb_MessageBox.Show("請選擇圖片文件", "更新失敗")
                Else
                    If FileLen / 1024 > 20000 Then
                        WinFormsToWeb_MessageBox.Show("圖片文件過大", "更新失敗")
                    Else
                        Dim str() As String
                        str = Split(MyFile.FileName, ".")
                        If UCase(str(str.Length - 1)) <> "JPG" Then
                            WinFormsToWeb_MessageBox.Show("請選擇.jpg格式的圖片", "更新失敗")
                        Else
                            Dim MyString As String
                            MyFileCollection = Request.Files
                            MyFile = MyFileCollection(0)
                            FileLen = MyFile.ContentLength
                            Dim Input(FileLen) As Byte
                            MyStream = MyFile.InputStream
                            MyStream.Read(Input, 0, FileLen)
                            Dim Conn As Data.SqlClient.SqlConnection = New Data.SqlClient.SqlConnection(PublicConnectString)
                            Conn.Open()
                            Dim comm As Data.SqlClient.SqlCommand = New Data.SqlClient.SqlCommand()
                            comm.Connection = Conn
                            comm.CommandText = "Update ObjectList set imageS = @ProductImage where id='" & Session("ImageSetup_Id") & "'"
                            comm.Parameters.Add("@ProductImage", SqlDbType.Binary, FileLen).Value = Input
                            comm.ExecuteNonQuery()
                            Conn.Close()
                        End If
                    End If
                End If
            End If