ASP.net如何将图片保存到指定的路径
4个回答
展开全部
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("<script>alert('图片上传成功')</script>");
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("<script>alert('请选择gif和jpg格式的图兆嫌裤片!')</script>");
}
}
{
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("<script>alert('图片上传成功')</script>");
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("<script>alert('请选择gif和jpg格式的图兆嫌裤片!')</script>");
}
}
展开全部
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);
}
}
}
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("<script>alert('图片上传成功')</script>");
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("<script>alert('请选择gif和jpg格式的图片!')</script>");
}
}
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("<script>alert('图片上传成功')</script>");
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("<script>alert('请选择gif和jpg格式的图片!')</script>");
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
将你的目的路径放入上传控件的保存中
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询