Class FileUploadControl

Represents file upload control that allow users to select a file to upload to the server.

Inheritance
System.Object
FileUploadControl
Namespace: Mediachase.FileUploader.Web.UI
Assembly: Mediachase.FileUploader.dll
Version: 10.8.0
Syntax
public class FileUploadControl : CompositeControl
Remarks

The FileUploadControl integrates to the page, initializes file upload engine and uses file upload entry-point page to select file(s) (default uploadiframe.aspx). All another controls should be assigned with FileUploadControl via FileUploadControlID property.

The FileUploadControl control does not automatically save file(s) to the server after the user selects the file. The user should use FileUploadActions control and click the Upload button to begin upload. The FileUploadControl uploads the file(s) to temporary file storage without page submitting. You can upload file and edit related information together.

When you submit page the Files property of FileUploadControl control gets uploaded file information collection of FileStreamInfo. The FileStreamInfo class represents common information about file (FileName, Size, ContentTypeName) and reference to temporary storage (ProviderName, SessionUid, StreamUid). 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 request file stream from TempFileStorageProvider. 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 to save the uploaded file. The SaveAs (to local disk) method copies file from temporary storage to the specified directory. Therefore, 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.

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 primary key of row. The SaveAs (to sql table) method copies file from temporary storage to the specified row column. Therefore, the sql row should be created and column data type should be image (A variable-length stream of binary data).

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

Use Provider property to get current TempFileStorageProvider. Using TempFileStorageProvider you can work with TempFileStorage directly. For example: request file stream and realize save file to custom persistent file storage.

Constructors

FileUploadControl()

Initializes a new instance of the FileUploadControl class.

Declaration
public FileUploadControl()

Properties

BlockHeight

Gets or sets the height of the block.

Declaration
public string BlockHeight { get; set; }
Property Value
Type Description
System.String

The height of the block.

Files

Gets the files.

Declaration
public FileStreamInfo[] Files { get; }
Property Value
Type Description
FileStreamInfo[]

The files.

FilesNotUploadedQuestion

Gets or sets the question if some files are not uploaded before form submitting.

Declaration
public string FilesNotUploadedQuestion { get; set; }
Property Value
Type Description
System.String

The question.

Remarks

Displays the client confirm message dialog if files are not uploaded before form submitting. The user can press Ok button to upload file and submit form again or Cancel button to cancel files selection.

Set empty string, to disable question.

HasFiles

Gets a value indicating whether this instance has files.

Declaration
public bool HasFiles { get; }
Property Value
Type Description
System.Boolean

true if this instance has files; otherwise, false.

ModeType

Gets or sets the type of the mode.

Declaration
public FileUploadControlMode ModeType { get; set; }
Property Value
Type Description
FileUploadControlMode

The type of the mode.

Provider

Gets or sets the provider.

Declaration
public TempFileStorageProvider Provider { get; set; }
Property Value
Type Description
TempFileStorageProvider

The provider.

ScriptPath

Gets or sets the script path.

Declaration
public string ScriptPath { get; set; }
Property Value
Type Description
System.String

The script path.

SessionUid

Gets or sets the session uid.

Declaration
public Guid SessionUid { get; set; }
Property Value
Type Description
System.Guid

The session uid.

SessionUidKey

Gets the session uid key.

Declaration
public string SessionUidKey { get; protected set; }
Property Value
Type Description
System.String

The session uid key.

TempFileStorageProvider

Gets or sets the temp file storage provider.

Declaration
public string TempFileStorageProvider { get; set; }
Property Value
Type Description
System.String

The temp file storage provider.

Methods

MoveTo(FileStreamInfo, String)

Moves a specified file to a new location from the temporary folder.

Declaration
public void MoveTo(FileStreamInfo info, string destFileName)
Parameters
Type Name Description
FileStreamInfo info

The info.

System.String destFileName

The name of the saved file.

Remarks

The method moves an incoming file from the temporary folder to a new location. If you use a HDD file storage, MoveTo method will increase performance and decrease a disk space requirement.

To invoke MoveTo(String) method you must turn on an incoming file optimization.

Note: You can execute MoveTo method only once.

Exceptions
Type Condition
System.NotSupportedException

If incoming file optimization turn off.

OnInit(EventArgs)

Declaration
protected override void OnInit(EventArgs e)
Parameters
Type Name Description
System.EventArgs e

OnPreRender(EventArgs)

Raises the System.Web.UI.Control.PreRender event.

Declaration
protected override void OnPreRender(EventArgs e)
Parameters
Type Name Description
System.EventArgs e

An System.EventArgs object that contains the event data.

ReleaseAll()

Releases the session.

Declaration
public void ReleaseAll()

ReleaseFile(FileStreamInfo)

Releases the stream.

Declaration
public void ReleaseFile(FileStreamInfo Info)
Parameters
Type Name Description
FileStreamInfo Info

The info.

Render(HtmlTextWriter)

Writes the System.Web.UI.WebControls.CompositeControl content to the specified System.Web.UI.HtmlTextWriter object, for display on the client.

Declaration
protected override void Render(HtmlTextWriter writer)
Parameters
Type Name Description
System.Web.UI.HtmlTextWriter writer

An System.Web.UI.HtmlTextWriter that represents the output stream to render HTML content on the client.

RenderContents(HtmlTextWriter)

Renders the contents of the control to the specified writer. This method is used primarily by control developers.

Declaration
protected override void RenderContents(HtmlTextWriter writer)
Parameters
Type Name Description
System.Web.UI.HtmlTextWriter writer

A System.Web.UI.HtmlTextWriter that represents the output stream to render HTML content on the client.

SaveAs(FileStreamInfo, String)

Saves the contents of an uploaded file.

Declaration
public void SaveAs(FileStreamInfo Info, string Filename)
Parameters
Type Name Description
FileStreamInfo Info

The file stream info.

System.String Filename

The filename.

SaveAs(FileStreamInfo, String, String, String, SqlParameter[])

Saves the contents of an uploaded file into MS SQL database.

Declaration
public void SaveAs(FileStreamInfo Info, string ConnectionString, string TableName, string ColumnName, params SqlParameter[] PrimaryKeys)
Parameters
Type Name Description
FileStreamInfo Info

The file stream info.

System.String ConnectionString

A sql connection string that includes the source database name, and other parameters needed to establish the initial connection. The default value is an empty string.

System.String TableName

A table name for the table that the current file stream will encapsulate.

System.String ColumnName

A column name for the column that the current file stream will encapsulate.

System.Data.SqlClient.SqlParameter[] PrimaryKeys

A primary keys collection for the row that the current file stream will encapsulate.

Examples

Example saves a new file into MS SQL database named File with FileId:int and Data:image columns.

private void btnSubmit_ServerClick(object sender, System.EventArgs e)
{
	if(McFileUp.PostedFile!=null)
	{
		int FileId = 0;

		//TODO: Create a new record and assign FileId variable.

		McFileUp.PostedFile.SaveAs("Data source=(local);Initial catalog=TestDB;User Id=sa;Password=","File","Data",new SqlParameter("@FileId",FileId));
	}
}