button控件中的代码? string fullname = this.File1.PostedFile.FileName;
string filename = fullname.Substring(fullname.LastIndexOf("\\") + 1);
string type = fullname.Substring(fullname.LastIndexOf(".")+1);
if (type == "jpg" || type == "bmp" || type == "gif")
{
this.File1.PostedFile.SaveAs(Server.MapPath("photo") + "\\" + filename);
this.Image1.ImageUrl="photo/"+filename;
}
else
{ Response.Write("你选择的图片格式有错");
} 先拖放一个html控件file 在源代码中加上“runnat=sever”,
{ //链接SQL Server数据库
string sql = System.Configuration.ConfigurationManager.AppSettings["strConn"].ToString();
SqlConnection strConn = new SqlConnection(sql);
if ((biaoti.Text == "") || (neirong.Text.ToString() == ""))
{
Label1.Text = "标题或内容不能为空!";
}
else if (biaoti.Text.Length >= 100)
{
Label1.Text = "你的标题太长了!";
}
else
{
//string ConnStr = "select * from 公告表";
//SqlCommand MyCommand = new SqlCommand(ConnStr, strConn);
//SqlDataReader dr = MyCommand.ExecuteReader();
//dr.Read();
//int iMaxIDNew = dr.GetInt32(0) + 1;
//String strMaxID = iMaxIDNew.ToString();
//dr.Close();
string ls = "";
try
{
//string a = FileUpload1.PostedFile.FileName;
if (FileUpload1.PostedFile.FileName.Length != 0)
{
ls = "1";
} //说明有文件上传了
}
catch
{
ls = "0";//没有文件上传
} if (ls == "1")
{
System.IO.Stream fileDataStream = FileUpload1.PostedFile.InputStream; //得到文件大小
int fileLength = FileUpload1.PostedFile.ContentLength; //创建数组
byte[] fileData = new byte[fileLength];
//把文件流填充到数组
fileDataStream.Read(fileData, 0, fileLength);
//得到文件类型
string fileType = FileUpload1.PostedFile.ContentType;
string name = FileUpload1.PostedFile.FileName;
int i = name.LastIndexOf("."); string newext = name.Substring(i);
//将新记录插入到数据库中
//ConnStr = "insert into 公告表(标题,发布者,发布时间,内容,附件) VALUES( '" + biaoti.Text + "','" + fabuzhe.Text + "','" + shijian.Text + "', '" + neirong.Text + "','" + fileData + "')";
// MyCommand = new SqlCommand(ConnStr, strConn);
// MyCommand.ExecuteNonQuery();
// Label1.Text = "增加成功!";
SqlCommand command = new SqlCommand("INSERT INTO 公告表 (标题,发布者,发布时间,内容,附件,附件类型,类型)" + "VALUES (@标题,@发布者,@发布时间,@内容,@附件,@附件类型,@类型)", strConn);
command.Parameters.AddWithValue("@标题", biaoti.Text);
command.Parameters.AddWithValue("@发布者", fabuzhe.Text);
command.Parameters.AddWithValue("@发布时间", shijian.Text);
command.Parameters.AddWithValue("@内容", neirong.Text);
command.Parameters.AddWithValue("@附件", fileData);
command.Parameters.AddWithValue("@附件类型", newext);
command.Parameters.AddWithValue("@类型", DropDownList1.SelectedValue .ToString());
strConn.Open();
command.ExecuteNonQuery();
Label1.Text = "增加成功";
strConn.Close();
}
else
{
string ConnStr = "insert into 公告表(标题,发布者,发布时间,内容,类型) VALUES( '" + biaoti.Text + "','" + fabuzhe.Text + "','" + shijian.Text + "', '" + neirong.Text + "','"+DropDownList1 .SelectedValue .ToString ()+"')";
SqlCommand MyCommand = new SqlCommand(ConnStr, strConn);
strConn.Open();
MyCommand.ExecuteNonQuery();
Label1.Text = "增加成功!";
}
biaoti.Text = "";
neirong.Text = "";
strConn.Close(); }
{
if (FileUpload.HasFile) //判断上传控件中是否已经选择了文件
{
if (Path.GetExtension(FileUpload.FileName).ToLower()==".rar") //判断上传的文件的格式是否争正确
{
string strFileUploadPath = "Upload/"; //文件上传的路径
string strFileName = FileUpload.FileName; //文件名
string strFilePhysicalPath = Server.MapPath(strFileUploadPath + strFileName);//文件上传的全路径
FileUpload.SaveAs(strFilePhysicalPath); //上传文件
response.write("上传成功");//提示成功
}
else
response.write("文件类型不符合上传要求");
}
else
response.write("请选择上传文件");
}using System.IO;
开头别忘了