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”,
解决方案 »
- 求asp.net+ jquery开发例子
- 截取字符串。不要A标签。谢谢
- 50分求救 很急 请大家帮忙 关于ASP.NET
- 用DAAB插入数据的时候怎么写?
- 在DATAGRID里面放个textbox框,显示数据.直接修改后,点一下保存.这个页面的数据全部修改掉
- 如何控制DataGrid列的显示!~~~在线等!!!!
- 请问怎么样使另一个页面提交过来的参数设置为该页的全局变量啊?
- ASP.net 上传图片 Could not find a part of the path 'D:\司机信息照片\DSCN0228.JPG'
- 很简单的问题,困扰我好久,关于水晶报表不能显示图表的问题
- 关于文本操作
- 请问一下“SteamerTicket.mdb'不是一个有效的路径的问题
- 如何再上传文件的同时提交数据
{ //链接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;
开头别忘了