C#File控件问题
<%@ Page Language="C#" AutoEventWireup="true" %><script runat="server">
void Button_Click(object sender, EventArgs e)
{
HtmlInputFile file = (HtmlInputFile)place.FindControl("Filel");
if (Text1.Value == "")
{
Span1.InnerHtml = "Error:You must enter a file name.";
return;
}
if (file.PostedFile.ContentLength > 0)
{
try
{
file.PostedFile.SaveAs("H:\\ASP\\" + Text1.Value);
Span1.InnerHtml = "File uploaded successfully to" +
"<b>H:\\Asp\\" + Text1.Value + "</b> on the web server.";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving flie <b>H:\\ASP\\" +
Text1.Value + "</b><br>" + exc.ToString() + ".";
}
}
}
void page_Load(object sender, EventArgs e)
{
HtmlInputFile file = new HtmlInputFile();
file.ID = "File";
place.Controls.Clear();
place.Controls.Add(file);
}
</script>
<html xmlns="http://www. >
<head>
<title>HtmlInputFile Constructor Example</title>
</head>
<body>
<div>HtmlInputFile Constructo Exampler</div>
<form enctype="multipart/form-data" runat="server">//“form”缺少必需的属性“action”但我不懂在那里加
Specify the file to upload:
<asp:placeHolder ID ="place" runat ="server"/>
<p>
Save as file name(no path):
<input id ="Text1"
type="Text"
runat="server" />
</p>
<p>
<span id ="Span1"
style="font :8pt verdana;"
runat="server"/>
</p>
<p>
<input type ="button"
id ="Button1"
value="Upload"
onserverclick="Button_Click"
runat="server"/>
</p>
</form>
</body>
</html>
能运行