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 to use CancellationToken option while importing

Platform: JavaScript |
Control: ejSpreadSheet |
Published Date: March 15, 2018 |
Last Revised Date: May 22, 2019

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

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.

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