RadControls for ASP.NET AJAX If you need to perform additional actions on uploaded files before saving them (for example, if you are using
custom fields), or if you want to manipulate them in memory
without saving them, you can use the RadUpload server-side API. You can use the server-side API to
rename uploaded files or save them into a database, or other storage medium.
Caution |
|---|
If you set the TargetFolder or TargetPhysicalFolder property and
then use the server-side API to manipulate the uploaded files, you may end up with two copies of the uploaded files.
Be aware that any valid files will already be saved to the target folder. |
RadUpload provides two properties to access the uploaded files:
UploadedFiles contains all valid uploaded files.
InvalidFiles contains uploaded files that did not pass the
integrated validation or
server-side custom validation. If you are not using either of these features,
all uploaded files will appear in the UploadedFiles collection.
Both the UploadedFiles and InvalidFiles properties are of type
Telerik.Web.UI.UploadedFileCollection. Each file in the collection is an instance of the
UploadedFile class. The following table lists the members of the UploadedFile class:
Property or Method | Type (Return Type) | Description |
|---|
Properties |
ContentLength | integer | The size of the uploaded file, in bytes. |
ContentType | string | The MIME type of the uploaded file. |
FileName | string | The fully qualified name of the file on the client. (IE6 and some older browsers only).
To get a file name that is the
same for all browsers, use the GetName() method instead. |
InputStream | System.IO.Stream | A stream object that can be used to read the file contents. |
Methods |
GetName() | string | Returns the name of the uploaded file. |
GetNameWithoutExtension() | string | Returns the name of the uploaded file, without the file extension. |
GetExtension() | string | Returns the extension of the uploaded file, including the leading dot (".") |
SaveAs(string, [bool]) | none | Save the file to the location specified by the first parameter. The optional second parameter
specifies whether
to overwrite an existing file if it is found. |
GetFieldValue(string) | string | Retrieves a custom field added to
the uploaded file. |
GetIsFieldChecked(string) | boolean | Retrieves whether a custom check box
added to the
uploaded file was checked. |
Saving uploaded files
The following example illustrates how to save uploaded files to a location of your choice:
CopyASPX
<telerik:radupload id="RadUpload1" runat="server" />
<asp:Button runat="server" ID="Button1" Text="Submit" OnClick="Button1_Click" />
CopyC#
protected void Button1_Click(object sender, System.EventArgs e)
{
foreach (UploadedFile f in RadUpload1.UploadedFiles)
{
f.SaveAs( "c:\\uploaded files\\" + f.GetName(), true);
}
}
CopyVB.NET
Imports Telerik.Web.UI...
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Button1.Click
For Each f As UploadedFile In RadUpload1.UploadedFiles
f.SaveAs("c:\my files\" & f.GetName, True)
Next
End Sub
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, or process its
contents without saving.
The following example demonstrates how to insert the uploaded files into a database using OleDb:
CopyC#
using System.Data.OleDb;
using Telerik.Web.UI;
...
protected void Button1_Click(object sender, System.EventArgs e)
{
foreach (UploadedFile file in RadUpload1.UploadedFiles)
{
byte[] bytes = new byte[file.ContentLength];
file.InputStream.Read(bytes, 0, file.ContentLength);
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();
}
}
}
}
CopyVB.NET
Imports System.Data.OleDb
Imports Telerik.Web.UI
...
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) _
Handles Button1.Click
For Each file As UploadedFile In RadUpload1.UploadedFiles
Dim bytes(file.ContentLength - 1) As Byte
file.InputStream.Read(bytes, 0, file.ContentLength)
Dim connection As OleDbConnection = CreateConnection()
Try
Dim command As 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
See Also