FileUpload.SaveAs Method

Saves the contents of a file that is uploaded using the FileUpload control to a specified path on the Web server.

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

public:
void SaveAs (
	String^ filename
)
public void SaveAs (
	String filename
)
public function SaveAs (
	filename : String
)
Not applicable.

Parameters

filename

A string that specifies the full path to the location on the server in which to save the uploaded file.

Exception typeCondition

HttpException

filename is not a full path.

The SaveAs method saves the contents of a file that is uploaded using the FileUpload control to a specified path on the Web server.

The FileUpload control does not automatically save a file to the server after the user selects the file to upload. You must explicitly provide a control or mechanism to allow the user to submit the specified file. For example, you can provide a button that the user clicks to upload the file. The code that you write to save the specified file should call the SaveAs method, which saves the contents of a file to a specified path on the server. Typically, the SaveAs method is called in an event-handling method for an event that raises a post back to the server. For example, if you provide a button to submit a file, the code to save the file to the server could then be placed inside the event-handling method for the click event.

When you call the SaveAs method, you must specify the full path to the directory on the server in which to save the uploaded file. If you do not explicitly specify a path in your application code, an HttpException exception is thrown when a user attempts to upload a file. This behavior helps to keep the files on the server secure, by not allowing users to specify a path in which to save the files that they upload.

Before calling the SaveAs method, you should use the HasFile property to verify that the FileUpload control contains a file to upload. If the HasFile returns true, call the SaveAs method. If it returns false, display a message to the user indicating that the control does not contain a file. If you do not provide error-handling code to verify that a file exists, an attempt to save a nonexistent file throws an HttpException exception.

For a call to the SaveAs to work, the ASP.NET application must have write access to the directory on the server. There are two ways that the application can get write access. You can explicitly grant write access to the account under which the application is running, in the directory in which the uploaded files will be saved. Alternatively, you can increase the level of trust that is granted to the ASP.NET application. To get write access to the executing directory for the application, the application must be granted the AspNetHostingPermission object with the trust level set to the System.Web.AspNetHostingPermissionLevel.Medium value. Increasing the level of trust increases the application's access to resources on the server. Note that this is not a secure approach, because a malicious user who gains control of your application will also be able to run under this higher level of trust. It is a best practice to run an ASP.NET application in the context of a user with the minimum privileges that are required for the application to run. For more information on security in ASP.NET applications, see Basic Security Practices for Web Applications and ASP.NET Trust Levels and Policy Files.

The following code example demonstrates how to create a FileUpload control that performs error checking. Before the file is saved, the HasFile method is called to verify that a file to upload actually exists. In addition, the File.Exists method is called to check whether a file with the same name already exists in the path. If it does, the name of the file to upload is prefixed with a number before the SaveAs method is called. This prevents the existing file from being overwritten.

No code example is currently available or this language may not be supported.

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

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show: