Saving files on the Server Side

When you submit a file for uploading, the Files property of the FileUploadControl control gets the uploaded file information collection of the FileStreamInfo class. The FileStreamInfo class represents the common information about the file (Name, Size, ContentTypeName) and it also contains reference information to the temporary storage (ProviderName SessionUid, FileStreamUid). The file name that this property returns does not include the path to the file on the client.

The code that you write to save the specified file should call the SaveAs method or the request file stream from the TempFileStorageProvider. These SaveAs methods can save the content of a file to a specified path on the server or to a sql table row.

When you call the SaveAs (to local disk) method, you must specify the full path to the directory in which you want to save the uploaded file. The SaveAs (to local disk) method copies files from temporary storage to the specified directory.

Therefore, the ASP.NET application you are creating must have write access to the directory on the server.  There are two ways that the application you have written can get write access.

1)     You can explicitly grant write access to the account under which the application is running as well as to the directory in which the uploaded files will be saved.

2)     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 also increases the application's access to resources on the server. Note: 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.

When you call the SaveAs (to sql table) method, you must specify the connection string, table name, column name (for the column that the current file stream will encapsulate), and the primary key of the row. The SaveAs (to sql table) method copies a file(s) from temporary storage to the specified row column.  Therefore, the sql row should already be created and the column data type should be set to image (A variable-length stream of binary data).

Use the ReleaseFile or ReleaseAll method to release temporary file(s) after save file(s) to persistent storage.

Use the Provider property to get the current TempFileStorageProvider. Using TempFileStorageProvider you can work with the TempFileStorage directly. For example, you can request a file stream and then perform a save file to your own custom persistent file storage.

Use the ModeType property to define file upload behavior such as: IFrame, PopUp or Embedded. Note: IFrame is the default value.


Copyright 2007 Mediachase. All rights reserved.