protected void Button1_Click(object sender, EventArgs e)
{
string constr = ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
string fullname = FileUpload1.PostedFile.FileName;
FileInfo fi = new FileInfo(fullname);
string name = fi.Name;
string type = fi.Extension;
if (".gif" == type || ".jpg" == type || ".Gif" == type || ".JPG" == type)
{
//定义保存图片的路径
string savepath = Server.MapPath("~/image");
//保存上传的图片
FileUpload1.PostedFile.SaveAs(savepath + "\\" + name);
string picurl=savepath+"\\"+name;
Response.Write("");
try
{
SqlConnection conn = new SqlConnection(constr);
string instr = "insert into picture (picurl) values ('"+picurl+"') ";
conn.Open();
SqlCommand cmd = new SqlCommand(instr,conn);
cmd.ExecuteNonQuery();
Image1.ImageUrl = "image" + "/" + name;
}
finally
{
new SqlConnection().Close();
}
}
else
{
Response.Write("");
}
}
private
void
copyfile(string
sources,
string
dest)
{
directoryinfo
dinfo=new
directoryinfo(sources);//注,这里面传的是路径,并不是文件,所以不能保含带后缀的文件
foreach(filesysteminfo
f
in
dinfo.getfilesysteminfos())
{
//目标路径destname
=
目标文件夹路径
+
原文件夹下的子文件(或文件夹)名字
//path.combine(string
a
,string
b)
为合并两个字符串
string
destname
=
path.combine(dest,
fsi.name);
if
(f
is
fileinfo)//如果是文件就复制
{
file.copy(f.fullname,
destname,
true);//true代表可以覆盖同名文件
}
else//如果是文件夹就创建文件夹然后复制然后递归复制
{
directory.createdirectory(destname);
copyfile(f.fullname,
destname);
}
}
}
protected
void
Button1_Click(object
sender,
EventArgs
e)
{
string
constr
=
ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
string
fullname
=
FileUpload1.PostedFile.FileName;
FileInfo
fi
=
new
FileInfo(fullname);
string
name
=
fi.Name;
string
type
=
fi.Extension;
if
(".gif"
==
type
||
".jpg"
==
type
||
".Gif"
==
type
||
".JPG"
==
type)
{
//定义保存图片的路径
string
savepath
=
Server.MapPath("~/image");
//保存上传的图片
FileUpload1.PostedFile.SaveAs(savepath
+
"\\"
+
name);
string
picurl=savepath+"\\"+name;
Response.Write("");
try
{
SqlConnection
conn
=
new
SqlConnection(constr);
string
instr
=
"insert
into
picture
(picurl)
values
('"+picurl+"')
";
conn.Open();
SqlCommand
cmd
=
new
SqlCommand(instr,conn);
cmd.ExecuteNonQuery();
Image1.ImageUrl
=
"image"
+
"/"
+
name;
}
finally
{
new
SqlConnection().Close();
}
}
else
{
Response.Write("");
}
}
将你的目的路径放入上传控件的保存中