FileUpload.SaveAs(String) 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
将上传文件的内容保存到 Web 服务器上的指定路径。
public:
void SaveAs(System::String ^ filename);
public void SaveAs(string filename);
member this.SaveAs : string -> unit
Public Sub SaveAs (filename As String)
参数
- filename
- String
一个字符串,指定保存上传文件的服务器位置的完整路径。
例外
filename 不是完整路径。
示例
以下示例演示如何创建执行 FileUpload 错误检查的控件。 保存文件之前, HasFile 将调用该方法以验证要上传的文件是否存在。 此外, File.Exists 调用该方法来检查路径中是否存在具有相同名称的文件。 如果这样做,则调用方法之前 SaveAs ,要上传的文件的名称以数字为前缀。 这可以防止覆盖现有文件。
<%@ Page Language="C#" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>FileUpload.SaveAs Method Example</title>
<script runat="server">
protected void UploadButton_Click(object sender, EventArgs e)
{
// Before attempting to save the file, verify
// that the FileUpload control contains a file.
if (FileUpload1.HasFile)
// Call a helper method routine to save the file.
SaveFile(FileUpload1.PostedFile);
else
// Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload.";
}
void SaveFile(HttpPostedFile file)
{
// Specify the path to save the uploaded file to.
string savePath = "c:\\temp\\uploads\\";
// Get the name of the file to upload.
string fileName = FileUpload1.FileName;
// Create the path and file name to check for duplicates.
string pathToCheck = savePath + fileName;
// Create a temporary file name to use for checking duplicates.
string tempfileName = "";
// Check to see if a file already exists with the
// same name as the file to upload.
if (System.IO.File.Exists(pathToCheck))
{
int counter = 2;
while (System.IO.File.Exists(pathToCheck))
{
// if a file with this name already exists,
// prefix the filename with a number.
tempfileName = counter.ToString() + fileName;
pathToCheck = savePath + tempfileName;
counter ++;
}
fileName = tempfileName;
// Notify the user that the file name was changed.
UploadStatusLabel.Text = "A file with the same name already exists." +
"<br />Your file was saved as " + fileName;
}
else
{
// Notify the user that the file was saved successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully.";
}
// Append the name of the file to upload to the path.
savePath += fileName;
// Call the SaveAs method to save the uploaded
// file to the specified directory.
FileUpload1.SaveAs(savePath);
}
</script>
</head>
<body>
<h3>FileUpload.SaveAs Method Example</h3>
<form id="Form1" runat="server">
<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>
</form>
</body>
</html>
<%@ Page Language="VB" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
<title>FileUpload.SaveAs Method Example</title>
<script runat="server">
Sub UploadButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
' Before attempting to save the file, verify
' that the FileUpload control contains a file.
If (FileUpload1.HasFile) Then
' Call a helper method routine to save the file.
SaveFile(FileUpload1.PostedFile)
Else
' Notify the user that a file was not uploaded.
UploadStatusLabel.Text = "You did not specify a file to upload."
End If
End Sub
Sub SaveFile(ByVal file As HttpPostedFile)
' Specify the path to save the uploaded file to.
Dim savePath As String = "c:\temp\uploads\"
' Get the name of the file to upload.
Dim fileName As String = FileUpload1.FileName
' Create the path and file name to check for duplicates.
Dim pathToCheck As String = savePath + fileName
' Create a temporary file name to use for checking duplicates.
Dim tempfileName As String
' Check to see if a file already exists with the
' same name as the file to upload.
If (System.IO.File.Exists(pathToCheck)) Then
Dim counter As Integer = 2
While (System.IO.File.Exists(pathToCheck))
' If a file with this name already exists,
' prefix the filename with a number.
tempfileName = counter.ToString() + fileName
pathToCheck = savePath + tempfileName
counter = counter + 1
End While
fileName = tempfileName
' Notify the user that the file name was changed.
UploadStatusLabel.Text = "A file with the same name already exists." + "<br />" + _
"Your file was saved as " + fileName
Else
' Notify the user that the file was saved successfully.
UploadStatusLabel.Text = "Your file was uploaded successfully."
End If
' Append the name of the file to upload to the path.
savePath += fileName
' Call the SaveAs method to save the uploaded
' file to the specified directory.
FileUpload1.SaveAs(savePath)
End Sub
</script>
</head>
<body>
<h3>FileUpload.SaveAs Method Example</h3>
<form id="Form1" runat="server">
<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>
</form>
</body>
</html>
注解
该方法 SaveAs 将上传的文件的内容保存到 Web 服务器上的指定路径。
当用户选择要上传的文件后,该 FileUpload 控件不会自动将文件保存到服务器。 必须显式提供一个控件或机制,以允许用户提交指定的文件。 例如,可以提供用户单击以上传文件的按钮。 写入以保存指定文件的代码应调用 SaveAs 该方法,该方法会将文件的内容保存到服务器上的指定路径。 通常,该方法 SaveAs 在事件处理方法中调用,该方法用于将回发回服务器的事件。 例如,如果提供用于提交文件的按钮,则可在单击事件的事件处理方法中包含将文件保存到服务器的代码。
调用 SaveAs 该方法时,必须在保存上传文件的服务器上指定目录的完整路径。 如果未在应用程序代码中显式指定路径, HttpException 则当用户尝试上传文件时会引发异常。 此行为可帮助保护服务器上的文件安全,不允许用户指定保存上传的文件的路径。
在调用 SaveAs 该方法之前,应使用 HasFile 属性来验证控件是否 FileUpload 包含要上传的文件。
HasFile如果返回true,则调用该方法SaveAs。 如果返回 false,则向用户显示一条消息,指示控件不包含文件。 如果未提供错误处理代码来验证文件是否存在,则尝试保存不存在的文件将 HttpException 引发异常。
若要使调用 SaveAs 正常工作,ASP.NET 应用程序必须对服务器上的目录具有写入访问权限。 应用程序可以通过两种方式获取写入访问权限。 可以在上传文件的目录中显式授予对运行应用程序的帐户的写入访问权限。 或者,可以增加授予 ASP.NET 应用程序的信任级别。 若要获取对应用程序的执行目录的写入访问权限,必须向应用程序授予 AspNetHostingPermission 该对象,并将信任级别设置为 AspNetHostingPermissionLevel.Medium 该值。 增加信任级别会增加应用程序对服务器上的资源的访问。 请注意,这不是一种安全的方法,因为获得应用程序控制权的恶意用户也能够在此更高的信任级别下运行。 最佳做法是在具有应用程序运行所需的最低权限的用户的上下文中运行 ASP.NET 应用程序。 有关 ASP.NET 应用程序中的安全性的详细信息,请参阅 Web 应用程序和 ASP.NET 信任级别和策略文件的基本安全做法。