public string UpImg()
{
string filetype;
filetype=File1.PostedFile.ContentType;
if(File1.PostedFile.FileName=="")
{
return "noimg";
} if(filetype!="image/gif" && filetype!="image/pjpg " && filetype!="image/pjpeg" && filetype!="image/bmp")
{
Response.Write("<script>alert('对不起!你上传的应该是图片!')</script>");
return "error";
}
else
{
System.Drawing.Image img= System.Drawing.Image.FromStream(File1.PostedFile.InputStream);
int Width = img.Width;
int Height = img.Height;
if(Width>200 || Height>100 || File1.PostedFile.ContentLength>1024*1024*2)
{
Response.Write("<script>alert('对不起!文件尺寸或大小不对!')</script>");
return "error";
}
else
{
int L;
L=File1.PostedFile.FileName.Length;
L=L-4;
filetype=File1.PostedFile.FileName.Substring(L,4);
string sPath=Server.MapPath("")+"\\"+"img"+"\\";
sPath+=Session["userid"].ToString()+house.GetDateTime().Replace(" ",null)+filetype;
this.File1.PostedFile.SaveAs(sPath);
Response.Write("<script>alert('图片上传成功!')</script>");
return "\\"+"img"+"\\"+Session["userid"].ToString()+house.GetDateTime().Replace(" ",null)+filetype;
} }

解决方案 »

  1.   

    这是我以前写的,图片不能大于2M,文件名为上传者的ID+日期+时间,存在网站目录里的IMG目录!可以判断是否为GIF或JPG
      

  2.   

    http://www.pconline.com.cn/pcedu/empolder/net/0309/215678.html
      

  3.   

    <% @Page Language="C#" %>
    <% @Import Namespace="System.IO" %>
    <% @ Import Namespace="System.Data" %>
    <% @ Import Namespace="System.Data.SqlClient" %><script runat="server">
    public void UploadBtn_Click (Object sender, EventArgs e){
    //FileStream fs = new FileStream(MyFile.Value, FileMode.OpenOrCreate, FileAccess.Read);

    //byte[] MyData= new byte[fs.Length];
    //fs.Read(MyData, 0, System.Convert.ToInt32(fs.Length));

    //fs.Close();
    //得到提交的文件
    Stream fileDataStream = MyFile.PostedFile.InputStream;//得到文件大小
    int fileLength = MyFile.PostedFile.ContentLength;//创建数组
    byte[] fileData = new byte[fileLength];//把文件流填充到数组
    fileDataStream.Read(fileData,0,fileLength);//得到文件名字
    string fileTitle = MyFileName.Value;//得到文件类型
    string fileType = MyFile.PostedFile.ContentType;//构建数据库连接,SQL语句,创建参数
    SqlConnection connection = new SqlConnection("Server=(local);uid=sa;pwd=;Database=pubs");
    SqlCommand command = new SqlCommand ("INSERT INTO TestFiles (MyFileName,MyFile,FileType)" + 
    "VALUES (@MyFileName,@MyFile,@FileType)", connection);SqlParameter paramTitle = new SqlParameter ("@MyFileName", SqlDbType.VarChar,35); 
    paramTitle.Value = fileTitle;
    command.Parameters.Add(paramTitle);SqlParameter paramData = new SqlParameter ("@MyFile", SqlDbType.Image);
    paramData.Value = fileData;
    command.Parameters.Add(paramData);SqlParameter paramType = new SqlParameter ("@FileType", SqlDbType.VarChar,25); 
    paramType.Value = fileType;
    command.Parameters.Add(paramType);//打开连接,执行查询
    connection.Open();
    command.ExecuteNonQuery();
    connection.Close();Message.Text="你的文件已经成功上载";
    MyFileName.Value = "";
    }
    </script>
    <hr>
    <asp:label id="Message" Text="选择文件和文件名字:" runat="server"/>
    <hr>
    <form method="post" enctype="multipart/form-data" runat="server">
    <b>文件名字:</b><input id="MyFileName" type="text" runat="server">
    <P>
    <b>文件:</b><input id="MyFile" type="file" runat="server"> 
    <br/><br/>
    <input type=submit value="开始上传" OnServerclick="UploadBtn_Click" runat="server">
    </form>
      

  4.   

    asp.net实现同时上传多个文件!(源码)namespace HowTos.MultipleImageUpdate
    {
    public class UPLOAD : System.Web.UI.Page
    {
    protected System.Web.UI.WebControls.LinkButton LinkButton1; #region User Defined Code

    protected System.Web.UI.WebControls.Label Label1;

    private void Page_Load(System.Object sender, System.EventArgs e)
    {
    if ( this.IsPostBack ) 
    this.SaveImages();
    } private System.Boolean SaveImages() {
    //loop through the files uploaded System.Web.HttpFileCollection _files = System.Web.HttpContext.Current.Request.Files;

    //Message to the user
    System.Text.StringBuilder _message = new System.Text.StringBuilder("Files Uploaded:<br>");

    try 
    {
    for ( System.Int32 _iFile = 0; _iFile < _files.Count; _iFile ++ ) 
    {

    // Check to make sure the uploaded file is a jpg or gif

    System.Web.HttpPostedFile _postedFile = _files[_iFile]; 
    System.String _fileName, _fileExtension; _fileName = System.IO.Path.GetFileName(
    _postedFile.FileName);

    _fileExtension = System.IO.Path.GetExtension(
    _fileName); if ( _fileExtension == ".gif" ) 
    {

    //Save File to the proper directory
    _postedFile.SaveAs( 
    System.Web.HttpContext.Current.Request.MapPath(
    "gifs/") + _fileName);
    _message.Append(_fileName + "<BR>");

    }
    else if ( _fileExtension == ".jpg" ) 
    { //Save File to the proper directory
    _postedFile.SaveAs( 
    System.Web.HttpContext.Current.Request.MapPath(
    "jpgs/") + _fileName);
    _message.Append(_fileName + "<BR>");


    else {

    _message.Append(_fileName + " <font color=\"red\">failed!! Only .gif and .jpg images allowed!</font> <BR>");

    } }

    Label1.Text = _message.ToString();
    return true;
    }
    catch ( System.Exception Ex ) 


    Label1.Text = Ex.Message ;
    return false;   

    } }
    #endregion #region Web Form Designer generated code
    override protected void OnInit(System.EventArgs e)
    {
    //
    // CODEGEN: This call is required by the ASP.NET Web Form Designer.
    //
    InitializeComponent();
    base.OnInit(e);
    }

    /// <summary>
    /// Required method for Designer support - do not modify
    /// the contents of this method with the code editor.
    /// </summary>
    private void InitializeComponent()
    {    
    this.Load += new System.EventHandler(this.Page_Load); }
    #endregion
    }
    }
      

  5.   

    1.inputfile(<input type=file id=inputfile runat=server>)
      可以设置inputfile.accept="image/gif,image/jpg",当然也可以写代码
     用inputfile.value.toupper.endwith("GIF") OR inputfile.value.toupper.endwith("JPG")
    2.dim i as integer=inputfile.value.lastindexof("/")
      dim j as integer=inputifle.value.indexof(".")
      dim str as string=inputfile.value.substring(i,j-i)
      dim tmp as string=inputfile.value.place(str,datatime.now.tostring)
    tmp为换了名后的全路径
    然后用inputfile.postedfile.savaas("路径"+inputfile.value.substring(i))
    3.用inputfile.size>2222做判断
     用response.write("<script>window.alert('error')</script>")提示
    4.在try中用inputfile.savaas方法,然后lblnotice.text="success"
      

  6.   

    System.Data.SqlClient改成OleDb就可以了