This documentation is archived and is not being maintained.

HttpPostedFile.SaveAs Method

Saves the contents of an uploaded file.

Namespace: System.Web
Assembly: System.Web (in system.web.dll)

public void SaveAs (
	string filename
)
public void SaveAs (
	String filename
)
public function SaveAs (
	filename : String
)

Parameters

filename

The name of the saved file.

The maximum size allowed for a request, which includes uploaded files, is 4 MB, by default. Maximum request size can be specified in the Machine.config or Web.config file in the maxRequestLength attribute of the httpRuntime Element (ASP.NET Settings Schema) element. The maximum request size for a specific page can be specified using the location Element (ASP.NET Settings Schema) element in a Web.config file.

The following code example demonstrates how to save all the files that are uploaded by the client to the C:\TempFiles folder on the Web server's local disk.

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);
 }
   

String tempFileName;
HttpFileCollection myFileCollection = get_Request().get_Files();

for (int loop1 = 0; loop1 < myFileCollection.get_Count(); loop1++) {
    // Create a new file name.
    tempFileName = "C:\\TempFiles\\File_" + String.valueOf(loop1);
    // Save the file.
    myFileCollection.get_Item(loop1).SaveAs(tempFileName);
}

var tempFileName : String
var myFileCollection : HttpFileCollection = Request.Files

for(var i=0; i < myFileCollection.Count; i++){
  // Create a new file name.
  tempFileName = "C:\TempFiles\File_" + i
  // Save the file.
  myFileCollection[i].SaveAs(tempFileName)
}


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see System Requirements.

.NET Framework

Supported in: 2.0, 1.1, 1.0
Show: