本节包含以下四个示例:
-
第一个示例演示如何创建 FileUpload 控件,该控件将文件保存到代码中指定的路径。
-
第二个示例演示如何创建 FileUpload 控件,该控件将文件保存到文件系统中针对应用程序的指定目录。
-
第三个示例演示如何创建 FileUpload 控件,该控件将文件保存到指定路径并限制可以上载的文件的大小。
-
第四个示例演示如何创建 FileUpload 控件,该控件将文件保存到指定路径并且只允许上载扩展名为 .doc 或 .xls 的文件。
|
---|
这些示例演示 FileUpload 控件的基本语法,但并没有演示保存文件之前应该完成的所有必要的错误检查。有关更完整的示例,请参见 SaveAs 。 |
下面的示例演示如何创建 FileUpload 控件,该控件将文件保存到代码中指定的路径。调用 SaveAs 方法将文件保存到服务器上的指定路径。
Visual Basic | 复制代码 |
---|
<%@ Page Language="VB"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<script runat="server"
>
Sub
UploadButton_Click(ByVal
sender As
Object, ByVal
e As
System.EventArgs)
Dim
savePath As
String = "c:/temp/uploads/"
If (FileUpload1.HasFile) Then
Dim
fileName As
String = FileUpload1.FileName
savePath += fileName
FileUpload1.SaveAs(savePath)
UploadStatusLabel.Text = "Your file was saved as "
& fileName
Else
UploadStatusLabel.Text = "You did not specify a file to upload."
End
If
End
Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head runat="server"
>
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server"
>
<div>
<h4>Select
a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server"
>
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server"
>
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server"
>
</asp:Label>
</div>
</form>
</body>
</html>
|
C# | 复制代码 |
---|
<%@ Page Language="C#"
%>
<!DOCTYPE html PUBLIC "-
<script runat="server"
>
protected
void
UploadButton_Click(object sender, EventArgs e)
{
String savePath = @"c:/temp/uploads/"
;
if
(FileUpload1.HasFile)
{
String fileName = FileUpload1.FileName;
savePath += fileName;
FileUpload1.SaveAs(savePath);
UploadStatusLabel.Text = "Your file was saved as "
+ fileName;
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload."
;
}
}
</script>
<html xmlns="http:
<head runat="server"
>
<title>FileUpload Example</title>
</head>
<body>
<form id="form1" runat="server"
>
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server"
>
</asp:FileUpload>
<br /><br />
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server"
>
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server"
>
</asp:Label>
</div>
</form>
</body>
</html>
|
下面的示例演示如何创建 FileUpload 控件,该控件将文件保存到文件系统中针对应用程序的指定目录。使用 HttpRequest. . :: . PhysicalApplicationPath 属性来获取当前正在执行的服务器应用程序的根目录的物理文件系统路径。调用 SaveAs 方法将文件保存到服务器上的指定路径。
Visual Basic | 复制代码 |
---|
<%@ Page Language="VB"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<script runat="server"
>
Sub
UploadButton_Click(ByVal
sender As
Object, ByVal
e As
System.EventArgs)
Dim
saveDir As
String = "/Uploads/"
Dim
appPath As
String = Request.PhysicalApplicationPath
If (FileUpload1.HasFile) Then
Dim
savePath As
String = appPath + saveDir + _
Server.HtmlEncode(FileUpload1.FileName)
FileUpload1.SaveAs(savePath)
UploadStatusLabel.Text = "Your file was uploaded successfully."
Else
UploadStatusLabel.Text = "You did not specify a file to upload."
End
If
End
Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head runat="server"
>
<title>FileUpload Class
Example</title>
</head>
<body>
<h3>FileUpload Class
Example: Save To
Application Directory</h3>
<form id="form1" runat="server"
>
<div>
<h4>Select
a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server"
>
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server"
>
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server"
>
</asp:Label>
</div>
</form>
</body>
</html>
|
| |
---|
<%@ Page Language="C#"
%>
<!DOCTYPE html PUBLIC "-
<script runat="server"
>
protected
void
UploadButton_Click(object sender, EventArgs e)
{
string saveDir = @"/Uploads/"
;
string appPath = Request.PhysicalApplicationPath;
if
(FileUpload1.HasFile)
{
string savePath = appPath + saveDir +
Server.HtmlEncode(FileUpload1.FileName);
FileUpload1.SaveAs(savePath);
UploadStatusLabel.Text = "Your file was uploaded successfully."
;
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload."
;
}
}
</script>
<html xmlns="http:
<head runat="server"
>
<title>FileUpload Class Example</title>
</head>
<body>
<h3>FileUpload Class Example: Save To Application Directory</h3>
<form id="form1" runat="server"
>
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server"
>
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server"
>
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server"
>
</asp:Label>
</div>
</form>
</body>
</html>
|
下面的示例演示如何创建 FileUpload 控件,该控件将文件保存到代码中指定的路径。该控件将上载文件的大小限制为 5 MB。使用 PostedFile 属性来访问基础 ContentLength 属性并返回文件的大小。如果要上载的文件的大小小于 2 MB,则调用 SaveAs 方法将文件保存到服务器上的指定路径。除了检查应用程序代码中的最大文件大小设置之外,您还可以将 httpRuntime 元素的 maxRequestLength 属性设置为应用程序配置文件中所允许的最大大小。
| |
---|
<%@ Page Language="C#"
%>
<!DOCTYPE html PUBLIC "-
<script runat="server"
>
protected
void
UploadButton_Click(object sender, EventArgs e)
{
string savePath = @"c:/temp/uploads/"
;
if
(FileUpload1.HasFile)
{
int fileSize = FileUpload1.PostedFile.ContentLength;
if
(fileSize < 2100000)
{
savePath += Server.HtmlEncode(FileUpload1.FileName);
FileUpload1.SaveAs(savePath);
UploadStatusLabel.Text = "Your file was uploaded successfully."
;
}
else
{
UploadStatusLabel.Text = "Your file was not uploaded because "
+
"it exceeds the 2 MB size limit."
;
}
}
else
{
UploadStatusLabel.Text = "You did not specify a file to upload."
;
}
}
</script>
<html xmlns="http:
<head runat="server"
>
<title>FileUpload Class Example</title>
</head>
<body>
<form id="form1" runat="server"
>
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server"
>
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadButton"
Text="Upload file"
OnClick="UploadButton_Click"
runat="server"
>
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server"
>
</asp:Label>
</div>
</form>
</body>
</html>
|
下面的示例演示如何创建 FileUpload 控件,该控件将文件保存到代码中指定的路径。该示例只允许上载扩展名为 .doc 或 .xls 的文件。调用 Path. . :: . GetExtension 方法来返回要上载的文件的扩展名。如果文件扩展名为 .doc 或 .xls,则调用 SaveAs 方法将文件保存到服务器上的指定路径。
Visual Basic | 复制代码 |
---|
<%@ Page Language="VB"
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"
>
<script runat="server"
>
Protected
Sub
UploadBtn_Click(ByVal
sender As
Object, ByVal
e As
System.EventArgs)
Dim
savePath As
String = "c:/temp/uploads/"
If (FileUpload1.HasFile) Then
Dim
fileName As
String = Server.HtmlEncode(FileUpload1.FileName)
Dim
extension As
String = System.IO.Path.GetExtension(fileName)
If (extension = ".doc") Or (extension = ".xls"
) Then
savePath += fileName
FileUpload1.SaveAs(savePath)
UploadStatusLabel.Text = "Your file was uploaded successfully."
Else
UploadStatusLabel.Text = "Your file was not uploaded because "
+ _
"it does not have a .doc or .xls extension."
End
If
Else
UploadStatusLabel.Text = "You did not specify a file to upload."
End
If
End
Sub
</script>
<html xmlns="http://www.w3.org/1999/xhtml"
>
<head runat="server"
>
<title>FileUpload Class
Example</title>
</head>
<body>
<form id="form1" runat="server"
>
<div>
<h4>Select
a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server"
>
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadBtn"
Text="Upload file"
OnClick="UploadBtn_Click"
runat="server"
>
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server"
>
</asp:Label>
</div>
</form>
</body>
</html>
|
| |
---|
<%@ Page Language="C#"
%>
<!DOCTYPE html PUBLIC "-
<script runat="server"
>
protected
void
UploadBtn_Click(object sender, EventArgs e)
{
string savePath = @"c:/temp/uploads"
;
if
(FileUpload1.HasFile)
{
string fileName = Server.HtmlEncode(FileUpload1.FileName);
string extension = System.IO.Path.GetExtension(fileName);
if
((extension == ".doc") | (extension == ".xls"
))
{
savePath += fileName;
FileUpload1.SaveAs(savePath);
UploadStatusLabel.Text = "Your file was uploaded successfully."
;
}
else
{
UploadStatusLabel.Text = "Your file was not uploaded because "
+
"it does not have a .doc or .xls extension."
;
}
}
}
</script>
<html xmlns="http:
<head runat="server"
>
<title>FileUpload Class Example</title>
</head>
<body>
<form id="form1" runat="server"
>
<div>
<h4>Select a file to upload:</h4>
<asp:FileUpload id="FileUpload1"
runat="server"
>
</asp:FileUpload>
<br/><br/>
<asp:Button id="UploadBtn"
Text="Upload file"
OnClick="UploadBtn_Click"
runat="server"
>
</asp:Button>
<hr />
<asp:Label id="UploadStatusLabel"
runat="server"
>
</asp:Label>
</div>
</form>
</body>
</html>
|