Articles in this section
Category / Section

How can I communicate with UploadBox from within the ashx file ?

2 mins read

Description

By using the uploadbox client-side events and its event argument, we can easily communicate information with server.

Solution

With help of client side events - ClientSideOnBegin, ClientSideOnComplete and its event arguments, we can easily communicate with the server. Please refer the bellow sample snippet.

1)Add the Upload box control in the ASPX page and define the Client side events ClientSideOnBegin and ClientSideOnComplete event.

 
<ej:UploadBox ID="Upload1" runat="server" UploadName="uploader"  ClientSideOnBegin="begin" ClientSideOnComplete="complete" SaveUrl="saveFiles.ashx" RemoveUrl="removeFiles.ashx"></ej:UploadBox>
 

2)Add the additional data to the ‘args.data’ (in ClientSideOnBeginEvent-event argument) for sending information to server and receive server response in ‘args.responseText’ (in ClientSideOnCompleteEvent-event argument).

 
<script type="text/javascript">
        function begin(args) {
 
            args.data = "sample data";
            //
            //set args.data for sending data to server.
            //
        }
 
        function complete(args)
        {
            // args.responseText - will holds the response from the server.
        }
 
</script>
 

 

3)Using context.Request.Form["UploadName_data"] – statement, we have to receive the client side data in “saveFiles.ashx“ -generic handler and write server response data using the context.Response.Write() Method.

 
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
 
namespace WebApplication1
{
    /// <summary>
    /// Summary description for saveFiles
    /// </summary>
    public class saveFiles : IHttpHandler
    {
 
        public void ProcessRequest(HttpContext context)
        {
            string targetFolder = HttpContext.Current.Server.MapPath("uploadfiles");
            string dataFromClient = context.Request.Form["uploader_data"];
            // context.Request.Form["UploadName_data"] uploadbox- UploadName property will be mapped. 
            if (!Directory.Exists(targetFolder))
            {
                Directory.CreateDirectory(targetFolder);
            }
            HttpRequest request = context.Request;
            HttpFileCollection uploadedFiles = context.Request.Files;
            if (uploadedFiles != null && uploadedFiles.Count > 0)
            {
                for (int i = 0; i < uploadedFiles.Count; i++)
                {
                    string fileName = uploadedFiles[i].FileName;
                    int indx = fileName.LastIndexOf("\\");
                    if (indx > -1)
                    {
                        fileName = fileName.Substring(indx + 1);
                    }
                    uploadedFiles[i].SaveAs(targetFolder + "\\" + fileName);
                }
            }
            else
            {
 
            }
            context.Response.ContentType = "text/plain";
            context.Response.Write("File has been received");
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied