Important notes
The selected files must be transferred to the server in order to be validated for size or mime-type. The file extensions can be validated on the client, before the upload.
To enable uploading of files, larger than 4MB, you must set the maxRequestLength attribute of the httpRuntime section in your web.config file.
Using the server-side API
If you need to perform additional actions on the uploaded files before saving them, you can use the server-side API. This way you can rename the uploaded files or save them into a database, or other storage medium.
Do not set TargetFolder or TargetPhysicalFolder if you decide to use the server-side API to manipulate the uploaded files. If you use both methods you may end with two copies of the uploaded files.
If the validation has been enabled, the uploaded files will be first validated. The valid files can be accessed using the UploadedFiles collection and the invalid files - using the InvalidFiles collection.
The most important methods and properties in the RadUpload server-side API are:
- RadUpload class
UploadedFiles - contains the uploaded files, which passed the validation, or all uploaded files if the validation was not enabled
InvalidFiles - contains the uploaded files, which did not passed the validation
- UploadedFile class
ContentLength - returns the size of the uploaded file in bytes
FileName - returns the full client-side path of the file (IE6 and some older browsers only). To get equal behavior with all browsers you should use GetName() method to obtain the name of the uploaded file.
GetName() - returns the name of the uploaded file
GetNameWithoutExtension() - returns the name of the uploaded file, without the extension
GetExtension() - returns the extension of the uploaded file, including the leading dot (.)
InputStream - provides access to the file content
SaveAs() - saves the uploaded file to the specified location
Using the SaveAs() method
This example below demonstrates how to save the uploaded files to a location of your choice:
ASPX/ASCX
<rad:radupload ID="RadUpload1" Runat="server" /> <asp:button Runat="server" ID="Button1" Text="Submit" onclick="Button1_Click" /> |
VB.NET
Imports Telerik.WebControls ... Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) For Each f As UploadedFile In RadUpload1.UploadedFiles f.SaveAs("c:\my files\" & f.GetName, True) Next End Sub |
C#
using Telerik.WebControls; ... protected void Button1_Click(object sender, System.EventArgs e) { foreach (UploadedFile f in RadUpload1.UploadedFiles) { f.SaveAs("c:\\my files\\" + f.GetName(), true); } } |
Using the InputStream property
You can use the InputStream property to access the content of the uploaded files without saving them to a temporary location. This property is useful when you want to insert the file into a database.
The following example demonstrates how to insert the uploaded files into a database using OleDb:
VB.NET
Imports System.Data.OleDb Imports Telerik.WebControls ... Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) For Each file As UploadedFile In RadUpload1.UploadedFiles Dim bytes(file.InputStream.Length) As Byte file.InputStream.Read(bytes, 0, file.InputStream.Length)
Dim connection As OleDbConnection = CreateConnection() Try Dim command As OleDbCommand = New OleDbCommand( "INSERT INTO Images ([Name], [Size], [Content]) VALUES (?, ?, ?)", connection) command.Parameters.AddWithValue("@Name", file.GetName) command.Parameters.AddWithValue("@Size", bytes.Length) command.Parameters.AddWithValue("@Content", bytes)
connection.Open() command.ExecuteNonQuery() Finally If (connection.State = Data.ConnectionState.Open) Then connection.Close() End If End Try Next End Sub |
C#
using System.Data.OleDb; using Telerik.WebControls; ... protected void Button1_Click(object sender, System.EventArgs e) { foreach (UploadedFile file in RadUpload1.UploadedFiles) { byte[] bytes = new byte[file.InputStream.Length]; file.InputStream.Read(bytes, 0, file.InputStream.Length);
OleDbConnection connection = CreateConnection() try { OleDbCommand command = New OleDbCommand( "INSERT INTO Images ([Name], [Size], [Content]) VALUES (?, ?, ?)", connection); command.Parameters.AddWithValue("@Name", file.GetName()); command.Parameters.AddWithValue("@Size", bytes.Length); command.Parameters.AddWithValue("@Content", bytes);
connection.Open(); command.ExecuteNonQuery(); } finally { if (connection.State == Data.ConnectionState.Open) { connection.Close(); } } } } |