We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: ASP.NET Web Forms |
Control: Uploadbox |
Published Date: May 2, 2017 |
Last Revised Date: May 9, 2019
Tags: uploadbox

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;
            }
        }
    }
}

2X faster development

The ultimate ASP.NET Web Forms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon