ASP.NET依托.net framework类库,封装了大量的功能,使得上传文件非常简单,主要有以下三种基本方法。
方法一:用Web控件FileUpload,上传到网站根目录。
Test.aspx关键代码:
- <form id="form1" runat="server">
- <asp:FileUpload ID="FileUpload1" runat="server" />
- <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
- <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>
- </form>
<form id="form1" runat="server">
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>
</form>
Test.aspx.cs关键代码:
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (FileUpload1.HasFile)
- {
- FileUpload1.SaveAs(Server.MapPath("~/") + FileUpload1.FileName);
- Label1.Text = "上传成功!";
- }
- }
protected void Button1_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
FileUpload1.SaveAs(Server.MapPath("~/") + FileUpload1.FileName);
Label1.Text = "上传成功!";
}
}
方法二:用Html控件HtmlInputFile,上传到网站根目录。
Test.aspx关键代码:
- <form id="form1" runat="server">
- <input type="file" id="file1" runat="server" />
- <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
- <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>
- </form>
<form id="form1" runat="server">
<input type="file" id="file1" runat="server" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>
</form>
Test.aspx.cs关键代码:
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (file1.PostedFile.ContentLength > 0)
- {
- file1.PostedFile.SaveAs(Server.MapPath("~/") + Path.GetFileName(file1.PostedFile.FileName));
- Label1.Text = "上传成功!";
- }
- }
protected void Button1_Click(object sender, EventArgs e)
{
if (file1.PostedFile.ContentLength > 0)
{
file1.PostedFile.SaveAs(Server.MapPath("~/") + Path.GetFileName(file1.PostedFile.FileName));
Label1.Text = "上传成功!";
}
}
方法三:用Html元素<input type="file" …/>,通过Request.Files上传到网站根目录。
Test.aspx关键代码:
- <form id="form1" runat="server" enctype="multipart/form-data">
- <input type="file" name="file" />
- <asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
- <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>
- </form>
<form id="form1" runat="server" enctype="multipart/form-data">
<input type="file" name="file" />
<asp:Button ID="Button1" runat="server" Text="上传" OnClick="Button1_Click" />
<asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label>
</form>
Test.aspx.cs关键代码:
- protected void Button1_Click(object sender, EventArgs e)
- {
- if (Request.Files["file"].ContentLength > 0)
- {
- Request.Files["file"].SaveAs(Server.MapPath("~/") + Path.GetFileName(Request.Files["file"].FileName));
- Label1.Text = "上传成功!";
- }
- }
protected void Button1_Click(object sender, EventArgs e)
{
if (Request.Files["file"].ContentLength > 0)
{
Request.Files["file"].SaveAs(Server.MapPath("~/") + Path.GetFileName(Request.Files["file"].FileName));
Label1.Text = "上传成功!";
}
}
注意两个区别:
一:FileUpload.FileName获取客户端上传文件名(不带路径),而file1.PostedFile.FileName 和Request.Files["file"].FileName在不同浏览器下情况不同:IE8下获得的是客户端上传文件的完全限定名(带路径),谷歌、苹果等浏览器下则仍为文件名(不带路径)。
二:FileUpload控件有HasFile属性,用于判断用户是否选择了上传文件,而后面两种方法则需要通过判断上传文件大小ContentLength属性,当用户没有选择上传文件时,该属性值为0。
可以看出FileUpload封装程度更高,但灵活性也稍差。