Articles in this section
Category / Section

How to use CancellationToken option while importing

2 mins read

Description

This knowledge base explains how to cancel importing excel file when the specified time limit is exceeded in the Spreadsheet.

Solution

It can be achievable by sending cancellationToken option with import request in “Spreadsheet.Open()” method.

In the below code example, we have specified the time limit as three seconds and if excel file import process exceeds more than three seconds, the import request token will be cancelled using “CancellationToken.Cancel()” method.

 

HTML

 

<div id="Spreadsheet"></div>

 

JS

 

$("#Spreadsheet").ejSpreadsheet({
    importSettings:
    {
        importMapper: "Spreadsheet/Import",
    }
});

 

Web API

 

public string Import()
{
    // Get imported file here, which is imported from file upload control.
    var files = HttpContext.Current.Request.Files;
    ImportRequest importRequest = new ImportRequest();
    NameValueCollection form = HttpContext.Current.Request.Form;
    CancellationTokenSource source = new CancellationTokenSource();
    importRequest.CancellationToken = source;
            
    importRequest.FileStream = files[0].InputStream;
    // Task to handle import request.
    Task<string> importTask = Task.Factory.StartNew(() =>
    {
        return Spreadsheet.Open(importRequest);
    });
 
    // Task to wait for specified time.
    Task waitingTask = Task.Factory.StartNew(() =>
    {
        Thread.Sleep(3000);
    });
 
    // Cancel the token when timeout occurs.
    if (Task.WaitAny(importTask, waitingTask) == 1)
    {
        // Request cancellation on the token. 
        importRequest.CancellationToken.Cancel();
    }
 
    string result = importTask.Result;
    importRequest.CancellationToken.Dispose();
 
    // Returns the result.
    return result;
            
}

 

Razor

 

@(Html.EJ().Spreadsheet<object>("Spreadsheet")
    .ImportSettings(import =>
    {
        import.ImportMapper("Home/Import");
    })
)

 

C#

 

public string Import(ImportRequest importRequest)
{
    CancellationTokenSource source = new CancellationTokenSource();
    importRequest.CancellationToken = source;
    // Task to handle import request.
    Task<string> importTask = Task.Factory.StartNew(() =>
    {
        return Spreadsheet.Open(importRequest);
    });
 
    // Task to wait for specified time.
    Task waitingTask = Task.Factory.StartNew(() =>
    {
        Thread.Sleep(3000);
    });
 
    // Cancel the token when timeout occurs.
    if (Task.WaitAny(importTask, waitingTask) == 1)
    {
        // Request cancellation on the token. 
        importRequest.CancellationToken.Cancel();
    }
 
    string result = importTask.Result;
    importRequest.CancellationToken.Dispose();
 
    // Returns the result.
    return result;
}

 

ASPX

 

<ej:Spreadsheet ID="Spreadsheet" runat="server">
    <ImportSettings ImportMapper="SpreadsheetHandler.ashx" />
</ej:Spreadsheet>

 

ASHX.CS

 

public void ProcessRequest(HttpContext context)
        {
            var files = context.Request.Files;
            ImportRequest importRequest = new ImportRequest();
            NameValueCollection form = HttpContext.Current.Request.Form;
            CancellationTokenSource source = new CancellationTokenSource();
            importRequest.CancellationToken = source;
 
            importRequest.FileStream = files[0].InputStream;
            // Task to handle import request.
            Task<string> importTask = Task.Factory.StartNew(() =>
            {
                return Spreadsheet.Open(importRequest);
            });
 
            // Task to wait for specified time.
            Task waitingTask = Task.Factory.StartNew(() =>
            {
                Thread.Sleep(3000);
            });
 
            // Cancel the token when timeout occurs.
            if (Task.WaitAny(importTask, waitingTask) == 1)
            {
                // Request cancellation on the token. 
                importRequest.CancellationToken.Cancel();
            }
 
            string result = importTask.Result;
            importRequest.CancellationToken.Dispose();
            context.Response.Write(result);
        }

 

The following output is displayed as the result of above behavior,

 

spreadsheet cancellation token sample preview

Figure: Notify the user with message when file not loaded within the specified time limit

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