HttpPostedFile.SaveAs(String) 方法

定義

儲存已上傳檔案的內容。

public:
 void SaveAs(System::String ^ filename);
public void SaveAs(string filename);
member this.SaveAs : string -> unit
Public Sub SaveAs (filename As String)

參數

filename
String

儲存檔案的名稱。

例外狀況

RequireRootedSaveAsPath物件的HttpRuntimeSection性質設為 true,但filename並非絕對路徑。

範例

以下程式碼範例示範如何將客戶端上傳的所有檔案儲存到 Web 伺服器本機磁碟的 C:\TempFiles 資料夾。

String TempFileName;
 HttpFileCollection MyFileCollection = Request.Files;

 for (int Loop1 = 0; Loop1 < MyFileCollection.Count; Loop1++)
 {
    // Create a new file name.
    TempFileName = "C:\\TempFiles\\File_" + Loop1.ToString();
    // Save the file.
    MyFileCollection[Loop1].SaveAs(TempFileName);
 }
Dim Loop1 As Integer
 Dim TempFileName As String
 Dim MyFileCollection As HttpFileCollection = Request.Files
 
 For Loop1 = 0 To MyFileCollection.Count - 1
    ' Create a new file name.
    TempFileName = "C:\TempFiles\File_" & CStr(Loop1)
    ' Save the file.
    MyFileCollection(Loop1).SaveAs(TempFileName)
 Next Loop1

備註

預設允許的最大請求大小(包含上傳檔案)為 4 MB。 最大請求大小可在 Machine.configmaxRequestLength 元素的 屬性中,Web.config 檔案中指定。 特定頁面的最大請求大小可透過 Web.config 檔案中的 location 元素(ASP.NET 設定架構) 來指定。

適用於