Example [Visual Basic, C#, JScript] This example demonstrates how to use the HtmlInputFile control to create a simple file upload scenario. For this example to work properly, you need to create a directory called TEMP on your C: drive.[Visual Basic] <%@ Page Language="VB" AutoEventWireup="True" %><html> <head>
<script language="VB" runat="server"> Sub Button1_Click(Source As Object, e As EventArgs)
If Text1.Value = "" Then Span1.InnerHtml = "Error: you must enter a file name" Return End If
If Not (File1.PostedFile Is Nothing) Then Try File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value)) Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _ Text1.Value & "</b> on the Web server" Catch exc As Exception Span1.InnerHtml = "Error saving file <b>c:\temp\" & _ Text1.Value & "</b><br>" & exc.ToString() End Try End If End Sub 'Button1_Click </script>
[Visual Basic, C#, JScript] This example demonstrates how to use the HtmlInputFile control to create a simple file upload scenario. For this example to work properly, you need to create a directory called TEMP on your C: drive.[Visual Basic]
<%@ Page Language="VB" AutoEventWireup="True" %><html>
<head>
<script language="VB" runat="server">
Sub Button1_Click(Source As Object, e As EventArgs)
If Text1.Value = "" Then
Span1.InnerHtml = "Error: you must enter a file name"
Return
End If
If Not (File1.PostedFile Is Nothing) Then
Try
File1.PostedFile.SaveAs(("c:\temp\" & Text1.Value))
Span1.InnerHtml = "File uploaded successfully to <b>c:\temp\" & _
Text1.Value & "</b> on the Web server"
Catch exc As Exception
Span1.InnerHtml = "Error saving file <b>c:\temp\" & _
Text1.Value & "</b><br>" & exc.ToString()
End Try
End If
End Sub 'Button1_Click
</script>
</head>
<body>
<h3>HtmlInputFile Sample</h3>
<form enctype="multipart/form-data" runat="server">
Select File to Upload:
<input id="File1"
type="file"
runat="server">
<p>
Save as filename (no path):
<input id="Text1"
type="text"
runat="server">
<p>
<span id=Span1
style="font: 8pt verdana;"
runat="server" />
<p>
<input type=button
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">
</form>
</body>
</html>[C#]
<%@ Page Language="C#" AutoEventWireup="True" %><html>
<head>
<script language="C#" runat="server">
void Button1_Click(object Source, EventArgs e)
{
if (Text1.Value == "")
{
Span1.InnerHtml = "Error: you must enter a file name";
return;
}
if (File1.PostedFile != null)
{
try
{
File1.PostedFile.SaveAs("c:\\temp\\"+Text1.Value);
Span1.InnerHtml = "File uploaded successfully to <b>c:\\temp\\" +
Text1.Value + "</b> on the Web server";
}
catch (Exception exc)
{
Span1.InnerHtml = "Error saving file <b>c:\\temp\\" +
Text1.Value + "</b><br>" + exc.ToString();
}
}
}
</script>
</head>
<body>
<h3>HtmlInputFile Sample</h3>
<form enctype="multipart/form-data" runat="server">
Select File to Upload:
<input id="File1"
type="file"
runat="server">
<p>
Save as filename (no path):
<input id="Text1"
type="text"
runat="server">
<p>
<span id=Span1
style="font: 8pt verdana;"
runat="server" />
<p>
<input type=button
id="Button1"
value="Upload"
OnServerClick="Button1_Click"
runat="server">
</form>
</body>
</html>
为什么不自己翻MSDN