全部代码
private void save_Click(object sender, System.EventArgs e)
{
System.Web.HttpPostedFile imagefile=this.ImageFile.PostedFile;
int a=imagefile.ContentLength; string title=this.Title.Text.Trim();
string comefrom=this.ComeFrom.Text.Trim();
string content=this.Content.Text.Trim().Replace("\r\n","<br>").Replace("\n","");
int nnewsid;
int isimage;
if(this.modename=="add")
{
ConnIntID NewsIntId=new ConnIntID("bool_news_body","newsid");
nnewsid=NewsIntId.getID();
}
else
{
nnewsid=this.newsid;
} if(this.IsImage.Checked==true){
isimage=1;
}
else
isimage=0;
if(modename=="add")
{
conn=new Conn("select count(*) from bool_news_body where title='"+title+"'","simple");
if(((int)conn.ret)>0)
{
errflag=0;
}
}
else
{
conn=new Conn("select count(*) from bool_news_body where title='"+title+"' and newsid<>"+newsid,"simple");
if(((int)conn.ret)>0)
{
errflag=0;
}
}
if(title.Length==0) 
{
this.TitleError.Text="新闻标题为空";
}
else
{
if(errflag==0) 
{
this.TitleError.Text="标题已经存在";
}
}
if(content=="")
{
errflag=0;
this.ContentError.Text="新闻内容为空";
}
if(this.modename=="add"&&errflag==1)
{
string insertnewssql="insert into bool_news_body(newsid,title,content,comefrom,IsImage,LastDate,IsVisible,querytime)  values(" 
+ nnewsid.ToString() +",'"+title+"','"+content+"','"+comefrom +"',"+isimage.ToString()+",'"+DateTime.Now+"',1,0)";
conn=new Conn(insertnewssql,"noselect");
conn.CloseDatabase();
ConnIntID updateid=new ConnIntID("bool_news_body","newsid");
updateid.updateID();
}
else{
if(isimage==0&&errflag==1)
{
string updatenewssql="update bool_news_body set title='"+title+"',content='"+content+"',comefrom='"+comefrom+"',lastdate='"+DateTime.Now+"' where newsid="+nnewsid;
conn=new Conn(updatenewssql,"noselect");
conn.CloseDatabase();
}
else
{
if(errflag==1){
 string updatenewssql="update bool_news_body set title='"+title+"',content='"+content+"',comefrom='"+comefrom+"',isimage="+isimage.ToString()+
 ",lastdate='"+DateTime.Now+"' where newsid="+nnewsid;
 conn=new Conn(updatenewssql,"noselect");
 conn.CloseDatabase();
 }
}
}


if(isimage==1&&errflag==1)
{
// try
// {
//System.Web.HttpPostedFile imagefile=this.ImageFile.PostedFile;
if(this.ImageFile.PostedFile.ContentLength>0)
{
string imagefilename=this.ImageFile.PostedFile.FileName;
int imagesize=this.ImageFile.PostedFile.ContentLength;
string imagetype=this.ImageFile.PostedFile.ContentType;
Stream imageStream=this.ImageFile.PostedFile.InputStream;
byte [] input= new byte[imagesize];
int intStatues=imageStream.Read(input,0,imagesize);
string connstr=ConfigurationSettings.AppSettings["connectstring"];
SqlConnection connection = new SqlConnection(connstr);
SqlCommand command = new SqlCommand("update bool_news_body set newsimage=@input,newsimagetype=@imagetype where newsid=@nnewsid",connection);
SqlParameter paramImageFile = new SqlParameter("@input", SqlDbType.Image);
paramImageFile.Value = input;
command.Parameters.Add(paramImageFile);
SqlParameter paramImageType = new SqlParameter( "@imagetype", SqlDbType.VarChar,255);
paramImageType.Value = imagetype;
command.Parameters.Add( paramImageType );
SqlParameter paramNewsID = new SqlParameter( "@nnewsid", SqlDbType.Int);
paramNewsID.Value = nnewsid;
command.Parameters.Add(paramNewsID );
connection.Open();
int numRowsAffected = command.ExecuteNonQuery();
connection.Close(); 
}
else
{
conn=new Conn("update bool_news_body set isimage=0 where newsid="+nnewsid,"noselect");
conn.CloseDatabase();
}
// }
// catch(Exception err)
// {
// this.err.Text=err.ToString();
// conn=new Conn("update bool_news_body set isimage=0 where newsid="+nnewsid,"noselect");
// conn.CloseDatabase();
// }
}
if(errflag==1)
{
Response.Redirect("index.aspx?module=news&file=index");
}
}
}

解决方案 »

  1.   

    是不是忘了设置 form 的 encType="multipart/form-data"
      

  2.   

    <form id="NewFax" method="post" encType="multipart/form-data" runat="server">
    encType="multipart/form-data"   不能掉 ;
      

  3.   

    可能没有命名文件的后缀吧。否则按流式文件处理filenames = file1.PostedFile.FileName()
            mynumber = InStrRev(filenames, ".")
            houzhui = Mid(filenames, mynumber)        If Not (file1.PostedFile Is Nothing) Then
                Try                file1.PostedFile.SaveAs(filepath + tbtdmc.Text + houzhui)
      

  4.   

    <form id="NewFax" method="post" encType="multipart/form-data" runat="server">
    不能掉了 encType="multipart/form-data"
      

  5.   

    1.encType="multipart/form-data"第2种可能是你上传图片文件夹权限不够问题
      

  6.   

    <input file=.... runat=server>
    //runat=server 不能少