asp.net HTML文件上传标签
微软提供的控件//www.jb51.net/codes/9709.html
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>html文件上传标签</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" runat="server" />
<asp:Button id="btn_up" runat="server" Text="上传" OnClick="btn_up_Click" />
</div>
</form>
</body>
</html>
后台
protected void btn_up_Click(object sender, EventArgs e)
{
string spath = Server.MapPath("~/test/");
string filename = File1.PostedFile.FileName;
int idx = filename.LastIndexOf(@"\");
string shortname = filename.Substring(idx + 1);//获得文件名
this.File1.PostedFile.SaveAs(spath + shortname);
}
end
官方给出的使用方法:
需要在要目录下新建两个目录:upfile和upimg
添加一个FileUpload控件.一个Button.一个Image.一个Label
关键代码:
string name = FileUpload1.FileName;//获得上传文件的名字.
string size = FileUpload1.PostedFile.ContentLength.ToString();//文件大小.
string type = FileUpload1.PostedFile.ContentType;//文件类型.
string type2 = name.Substring(name.LastIndexOf(".") + 1);//LastIndexOf()最后一个索引位置匹配.Substring()里面的+1是重载.
string ipath = Server.MapPath("upimg") + "\\" + name;//取得根目录下面的upimg目录的路径.
string fpath = Server.MapPath("upfile") + "\\" + name;
string wpath = "upimg\\" + name;//获得虚拟路径.
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
{
FileUpload1.SaveAs(ipath);//保存方法,参数是一个地址字符串.
Image1.ImageUrl = wpath;
Label1.Text = "你传的文件名是:" + name + "<br>文件大小为:" + size + "字节<br>文件类型是:" + type +
"<br>后缀是:" + type2 + "<br>实际路径是:" + ipath + "<br>虚拟路径是:" + fpath;
Image1.Visible = true;
}
else
{
Image1.Visible = false;
FileUpload1.SaveAs(fpath);
Label1.Text = "你传的文件名是:" + name + "<br>文件大小为:" + size + "字节<br>文件类型是:" + type +
"<br>后缀是:" + type2 + "<br>实际路径是:" + ipath + "<br>虚拟路径是:" + fpath;
}
前台
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title>html文件上传标签</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<input id="File1" type="file" runat="server" />
<asp:Button id="btn_up" runat="server" Text="上传" OnClick="btn_up_Click" />
</div>
</form>
</body>
</html>
后台
protected void btn_up_Click(object sender, EventArgs e)
{
string spath = Server.MapPath("~/test/");
string filename = File1.PostedFile.FileName;
int idx = filename.LastIndexOf(@"\");
string shortname = filename.Substring(idx + 1);//获得文件名
this.File1.PostedFile.SaveAs(spath + shortname);
}
end
官方给出的使用方法:
需要在要目录下新建两个目录:upfile和upimg
添加一个FileUpload控件.一个Button.一个Image.一个Label
关键代码:
string name = FileUpload1.FileName;//获得上传文件的名字.
string size = FileUpload1.PostedFile.ContentLength.ToString();//文件大小.
string type = FileUpload1.PostedFile.ContentType;//文件类型.
string type2 = name.Substring(name.LastIndexOf(".") + 1);//LastIndexOf()最后一个索引位置匹配.Substring()里面的+1是重载.
string ipath = Server.MapPath("upimg") + "\\" + name;//取得根目录下面的upimg目录的路径.
string fpath = Server.MapPath("upfile") + "\\" + name;
string wpath = "upimg\\" + name;//获得虚拟路径.
if (type2 == "jpg" || type2 == "gif" || type2 == "bmp" || type2 == "png")
{
FileUpload1.SaveAs(ipath);//保存方法,参数是一个地址字符串.
Image1.ImageUrl = wpath;
Label1.Text = "你传的文件名是:" + name + "<br>文件大小为:" + size + "字节<br>文件类型是:" + type +
"<br>后缀是:" + type2 + "<br>实际路径是:" + ipath + "<br>虚拟路径是:" + fpath;
Image1.Visible = true;
}
else
{
Image1.Visible = false;
FileUpload1.SaveAs(fpath);
Label1.Text = "你传的文件名是:" + name + "<br>文件大小为:" + size + "字节<br>文件类型是:" + type +
"<br>后缀是:" + type2 + "<br>实际路径是:" + ipath + "<br>虚拟路径是:" + fpath;
}
相关推荐
Lzs 2020-10-23
聚合室 2020-11-16
零 2020-09-18
Justhavefun 2020-10-22
jacktangj 2020-10-14
ChaITSimpleLove 2020-10-06
Andrea0 2020-09-18
周游列国之仕子 2020-09-15
afanti 2020-09-16
88234852 2020-09-15
YClimb 2020-09-15
风雨断肠人 2020-09-04
卖口粥湛蓝的天空 2020-09-15
stulen 2020-09-15
pythonxuexi 2020-09-06
abfdada 2020-08-26
梦的天空 2020-08-25