方法二:用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>
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 = "上传成功!";
}
}