Skip to main content
.NET Framework Class Library
HttpPostedFile..::.SaveAs Method

Saves the contents of an uploaded file.

Namespace: System.Web
Assembly: System.Web (in System.Web.dll)
Syntax
Public Sub SaveAs ( _
	filename As String _
)
public void SaveAs(
	string filename
)
public:
void SaveAs(
	String^ filename
)
member SaveAs : 
        filename:string -> unit 

Parameters

filename
Type: System..::.String
The name of the saved file.
Exceptions
ExceptionCondition
HttpException

The RequireRootedSaveAsPath property of the HttpRuntimeSection object is set to true, but filename is not an absolute path.

Remarks

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.

Examples

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.


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
   


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

Version Information

.NET Framework

Supported in: 4, 3.5, 3.0, 2.0, 1.1, 1.0
Platforms

Windows 7, Windows Vista SP1 or later, Windows XP SP3, Windows XP SP2 x64 Edition, Windows Server 2008 (Server Core not supported), Windows Server 2008 R2 (Server Core supported with SP1 or later), Windows Server 2003 SP2

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