With DXperience v2010.1 we are introducing a couple of new Silverlight only controls. One of them is a long requested Upload Control
The Upload Control will provide a rich UI for adding and removing files, and a progress indicator where you can suspend or resume the upload operations.
Working with the Upload Control
To get started, we will need to reference the DevExpress.Xpf.Controls.v10.1. The control itself lives is in the DevExpress.Xpf.Controls namespace we can reference it in XAML using the new namespace prefix xmlns:controls=http://schemas.devexpress.com/winfx/2008/xaml/controls.
<controls:UploadControl
WebHandlerUri="http://localhost:62025/upload">
</controls:UploadControl>
The most important property that we need to set is WebHandlerUri. It should point to an HTTP handler that can process HTTP POST requests. By default, the UploadControl uses the HttpWebRequestUploadService to post data to the server, which internally uses System.Net.HttpWebRequest.
When the data is posted to the server, HttpWebRequestUploadService will decorate the URI with following query parameters:
- fileName: The name of the file as it appears on the client side
- packageCount: Total number of packages that are being uploaded. (UploadControl splits the file added into packages.)
- packageNumber: Index in into the package for that file.
If you are using ASP.NET, you might implement an IHttpHandler to handle the upload requests. Here is a small example of an IHttpHandler that receives files from the UploadControl into a “Received Files” folder.
public class UploadHandler : IHttpHandler {
const string filePath = "~/Received Files";
public bool IsReusable { get { return false; } }
string GetServerPath(HttpServerUtility server, string fileName) {
return server.MapPath(Path.Combine(filePath, Path.GetFileName(fileName)));
}
public void ProcessRequest(HttpContext context) {
string clientFileName = Uri.UnescapeDataString(context.Request.QueryString["fileName"]);
string serverFileName = GetServerPath(context.Server, clientFileName);
int packageCount = int.Parse(context.Request.QueryString["packageCount"]);
int packageNumber = int.Parse(context.Request.QueryString["packageNumber"]);
string serverPath = Path.GetDirectoryName(serverFileName);
if (!Directory.Exists(serverPath)) {
Directory.CreateDirectory(serverPath);
}
FileMode fileMode = (File.Exists(serverFileName) && packageNumber > 0) ?
FileMode.Append : FileMode.Create;
using (BinaryReader reader = new BinaryReader(context.Request.InputStream)) {
using (BinaryWriter writer = new BinaryWriter(File.Open(serverFileName, fileMode))) {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = reader.Read(buffer, 0, buffer.Length)) != 0)
writer.Write(buffer, 0, bytesRead);
}
}
}
}
Cheers,
Azret