protected void Button1_Click(object sender, EventArgs e)
{
try
{
String imgPath = FileUpload1.PostedFile.FileName;
String imgName = imgPath.Substring(imgPath.LastIndexOf("\\")+1);
String imgExtend = imgPath.Substring(imgPath.LastIndexOf(".")+1);
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData=new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData,0,FileLen);
SqlConnection con = new SqlConnection("server=(local);user id=zheng;pwd=zheng;database=aa");
con.Open();
SqlCommand com = new SqlCommand("insert into Files(FileData)values(@imgdata)",con);
com.Parameters.Add("@imgdata",SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
}
catch(Exception error)
{
Response.Write("上传失败"+error.ToString());
}
{
try
{
String imgPath = FileUpload1.PostedFile.FileName;
String imgName = imgPath.Substring(imgPath.LastIndexOf("\\")+1);
String imgExtend = imgPath.Substring(imgPath.LastIndexOf(".")+1);
int FileLen = this.FileUpload1.PostedFile.ContentLength;
Byte[] FileData=new Byte[FileLen];
HttpPostedFile hp = FileUpload1.PostedFile;
Stream sr = hp.InputStream;
sr.Read(FileData,0,FileLen);
SqlConnection con = new SqlConnection("server=(local);user id=zheng;pwd=zheng;database=aa");
con.Open();
SqlCommand com = new SqlCommand("insert into Files(FileData)values(@imgdata)",con);
com.Parameters.Add("@imgdata",SqlDbType.Image);
com.Parameters["@imgdata"].Value = FileData;
com.ExecuteNonQuery();
}
catch(Exception error)
{
Response.Write("上传失败"+error.ToString());
}
}
有错误大家指正..
本文介绍了一个使用ASP.NET实现文件上传并将其保存到数据库的过程。具体包括获取上传文件的信息,如路径、名称和扩展名;读取文件数据;连接数据库并执行插入操作等步骤。同时提供了一个简单的错误处理机制。
&spm=1001.2101.3001.5002&articleId=82271170&d=1&t=3&u=2aa1e48ad7544af694d6c6f8d90932b2)
7359

被折叠的 条评论
为什么被折叠?



