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 會將上傳檔案的內容儲存至網頁伺服器上的指定路徑。
當使用者選取要上傳的檔案之後,控件 FileUpload 不會自動將檔案儲存到伺服器。 您必須明確提供控件或機制,讓使用者提交指定的檔案。 例如,您可以提供使用者按兩下以上傳檔案的按鈕。 您撰寫以儲存指定檔案的程式代碼應該呼叫 SaveAs 方法,以將檔案的內容儲存到伺服器上的指定路徑。 一般而言,方法 SaveAs 會在事件處理方法中呼叫,以引發回傳至伺服器的事件。 例如,如果您提供一個按鈕來提交檔案,將檔案儲存至伺服器的程式代碼就可以包含在 Click 事件的事件處理方法中。
當您呼叫 SaveAs 方法時,您必須指定伺服器上要儲存上傳檔案之目錄的完整路徑。 如果您未在應用程式程式代碼中明確指定路徑, HttpException 當使用者嘗試上傳檔案時,就會擲回例外狀況。 此行為可讓使用者指定用來儲存其上傳檔案的路徑,協助保護伺服器上的檔案安全。
在呼叫 SaveAs 方法之前,您應該使用 HasFile 屬性來驗證 FileUpload 控件是否包含要上傳的檔案。 如果 傳HasFiletrue
回 ,請呼叫 SaveAs 方法。 如果傳回 false
,則向使用者顯示訊息,指出控件不包含檔案。 如果您未提供錯誤處理程式碼來驗證檔案是否存在,則嘗試儲存不存在的檔案會 HttpException 擲回例外狀況。
若要讓呼叫 SaveAs 運作,ASP.NET 應用程式必須具有伺服器上目錄的寫入許可權。 應用程式有兩種方式可以取得寫入許可權。 您可以在上傳的檔案儲存所在的目錄中,明確授與應用程式執行所在帳戶的寫入許可權。 或者,您可以增加授與給 ASP.NET 應用程式的信任層級。 若要取得應用程式執行目錄的寫入許可權,必須將信任層級設定為 AspNetHostingPermissionLevel.Medium 值的物件授AspNetHostingPermission與應用程式。 增加信任層級會增加應用程式對伺服器上的資源存取權。 請注意,這不是安全的方法,因為取得您應用程式控制權的惡意使用者也可以在這個較高層級的信任下執行。 最佳做法是在具有應用程式執行所需最低許可權的用戶內容中執行 ASP.NET 應用程式。 如需 ASP.NET 應用程式中安全性的詳細資訊,請參閱 Web 應用程式和 ASP.NET 信任層級和原則檔案的基本安全性作法。