Skip to main content

Server-Side Upload Service

  • 3 minutes to read

The DXUploadControl uses a System.Net.HttpWebRequest object to communicate with the server by performing HTTP requests via the POST method. Uploaded files are transmitted split across packages that contain data fragments with the file name and package number passed as query string parameters.

This data can be read by the server in multiple ways. The easiest way is to implement a web handler on the server side.

#Implementing a Web Handler

To implement a web handler on the server, perform the following steps.

  1. Add a web handler file to the web server project.

    uploadcontrol_Tutorial_AddNewItem

    UploadControl_AddingGenericWebHandler

  2. In the web handler template generated in the .ashx file, implement the ProcessRequest method to obtain and handle data sent by the client. To access the input stream that contains data, use the context parameter's HttpContext.Request.InputStream property.

    Note that the target file and directory names are passed as query string parameters. Use the HttpContext.Request.QueryString property to access the parameters collection.

    The following table lists all available query string parameters.

    Parameter Name Description
    filePath The path to the target directory where the received files should be saved.
    fileName The name of the currently transmitted file.
    packageCount Total number of packages in the current file.
    packageNumber The number of the currently transmitted package.

    A sample URL is shown below.

    http://localhost:54199/Handler1.ashx?filePath=Images%5CSamples&fileName=Snow.png&packageCount=10&packageNumber=3

    NOTE

    Query string parameters are generated and passed to the server automatically by the client UploadControl. Do not specify them manually in the web handler's URI assigned to the UploadControl.WebHandlerUri property.

    The following code illustrates how the ProcessRequest method can be implemented:

    
    public class UploadHandler : IHttpHandler {
    
        // ...
            public void ProcessRequest(HttpContext context) {
                var query = context.Request.QueryString;
                FilePath = Uri.UnescapeDataString(query["filePath"]);
                FileName = Uri.UnescapeDataString(query["fileName"]);
                PackageCount = int.Parse(query["packageCount"]);
                PackageNumber = int.Parse(query["packageNumber"]);
    
                string serverFileName = GetServerPath(context.Server, FilePath, FileName);
                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);
                }
            }
        }
    

    For a complete example, see How to: Implement a Web Handler for DXUploadControl.

  3. The web handler is now ready. To connect an Upload Control to the server, assign the web handler's URI and path to the target directory on the server to the UploadControl.WebHandlerUri and UploadControl.UploadServerPath properties, respectively, as shown above.

    
    uploadControl.WebHandlerUri =
        new System.Uri("http://localhost:54199/Handler1.ashx");
    uploadControl.UploadServerPath = "Images\\Samples";
    
    NOTE

    Web server's port is specified on the Project Properties page in the Web tab.

    UploadControl_Tutorial_SpecifyWebHandlerPort

The image below shows the result.

UploadControl

#Example