This documentation is archived and is not being maintained.

HttpPostedFile.SaveAs Method

Saves the contents of an uploaded file.

[Visual Basic]
Public Sub SaveAs( _
   ByVal filename As String _
)
[C#]
public void SaveAs(
 string filename
);
[C++]
public: void SaveAs(
 String* filename
);
[JScript]
public function SaveAs(
   filename : String
);

Parameters

filename
The name of the saved file.

Remarks

The maximum allowed uploaded file size is 8MB by default. Maximum file size can be specified in the machine.config or Web.config configuration files in the maxRequestLength attribute of the <httpRuntime> Element element. The maximum file size for a specific page can be specified using the <location> Element element in a Web.config file.

Example

The following example saves all the files uploaded by the client to a folder named "C:\TempFiles" on the Web server's local disk.

[Visual Basic] 
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
   

[C#] 
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);
 }
   

[C++] 
String* TempFileName;
 HttpFileCollection* MyFileCollection = Request->Files;
 
 for (int Loop1 = 0; Loop1 < MyFileCollection->Count; Loop1++)
 {
    // Create a new file name.
    TempFileName = String::Format( S"C:\\TempFiles\\File_{0}", __box(Loop1));
    // Save the file.
    MyFileCollection->Item[Loop1]->SaveAs(TempFileName);
 }
   

[JScript] 
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)
}

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

HttpPostedFile Class | HttpPostedFile Members | System.Web Namespace

Show: