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 handle custom error message on importing

Platform: JavaScript |
Control: ejSpreadSheet |
Published Date: March 7, 2018 |
Last Revised Date: May 22, 2019
Tags: custom-error

Description

This knowledge base explains how to handle custom error message on excel importing in Spreadsheet.

Solution

It can be achievable by using ‘actionBegin’ client-side event and ‘alert()’ client-side method.

In the below scenario, incorrect file path error has to be handled on importing using try-catch and on exception throws, the custom message is returned to Spreadsheet (here, returned ‘File not found.’ message).

 

JavaScript

 

HTML

<button id="importBtElem">Import</button>
<div id="FlatSpreadsheet"></div>

 

JS

$(function () {
    $("#FlatSpreadsheet").ejSpreadsheet({
        importSettings: {                    
            importMapper: "api/Spreadsheet/Import"
        },
        actionBegin: "onActionBegin"
    });
 
    $("#importBtElem").on("click", function () {
        var ssObj = $("#FlatSpreadsheet").data("ejSpreadsheet");
        ssObj.import({});//Import request to server.
    });
});
 
function onActionBegin(args) {
    if (args.reqType == "importing") {
        this.alert(args.data); //Alert the returned message in Spreadsheet.
        args.cancel = true;
    }
}

 

WEBAPI

[AcceptVerbs("Post")]
[OperationContract]
[WebGet(BodyStyle = WebMessageBodyStyle.Bare)]
public HttpResponseMessage Import()
{
    try
    {
        ImportRequest importRequest = new ImportRequest();
        //Trying to open incorrect file path.
        string filePath = HttpContext.Current.Server.MapPath("SampleExcel.xlsx");
        FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read);
        importRequest.FileStream = fileStream;
 
        string spreadsheetData = Spreadsheet.Open(importRequest);
        fileStream.Close();
        return new HttpResponseMessage() { Content = new StringContent(spreadsheetData, Encoding.UTF8, "text/plain") };
    }
    catch (Exception ex)
    {
        return new HttpResponseMessage() { Content = new StringContent("File not found.", Encoding.UTF8, "text/plain") };
    }
}

 

MVC

 

CSHTML

<button id="importBtElem">Import</button>
@(Html.EJ().Spreadsheet<object>("FlatSpreadsheet")
    .ImportSettings(import =>
    {
        import.ImportMapper("Home/Import");
    })
    .ClientSideEvents(eve => eve.ActionBegin("onActionBegin"))
)
<script type="text/javascript">
    $(function () {       
        $("#importBtElem").on("click", function () {
            var ssObj = $("#FlatSpreadsheet").data("ejSpreadsheet");
            ssObj.import({});//Import request to server.
        });
    });
 
    function onActionBegin(args) {
        if (args.reqType == "importing") {
            this.alert(args.data); //Alert the returned message in Spreadsheet.
            args.cancel = true;
        }
    }
</script>

 

CONTROLLER

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult Import(ImportRequest importRequest)
{
    try
    {
        //Trying to open incorrect file path.
        string filePath = Server.MapPath("SampleExcel.xlsx");
        FileStream fileStream = System.IO.File.Open(filePath, FileMode.Open, FileAccess.Read);
        importRequest.FileStream = fileStream;
 
        string spreadsheetData = Spreadsheet.Open(importRequest);
        fileStream.Close();
        return Content(spreadsheetData);
    }
    catch (Exception ex)
    {
        return Content("File not found.");
    }
}

 

ASP

 

ASPX

<button id="importBtElem" type="button">Import</button>
<ej:Spreadsheet ID="FlatSpreadsheet" runat="server">
    <ImportSettings ImportMapper="SpreadsheetHandler.ashx" />
    <ClientSideEvents ActionBegin="onActionBegin" />
</ej:Spreadsheet>
 
<script type="text/javascript">        
    $(function () {
        $("#importBtElem").on("click", function () {
            var ssObj = $("#" + '<% =FlatSpreadsheet.ClientID %>').data("ejSpreadsheet");
            ssObj.import({});//Import request to server.
        });
    });
 
    function onActionBegin(args) {
        if (args.reqType == "importing") {
            this.alert(args.data); //Alert the returned message in Spreadsheet.
            args.cancel = true;
        }
    }
</script>

 

ASHX

public void ProcessRequest(HttpContext context)
{
    try
    {
        ImportRequest importRequest = new ImportRequest();
        //Trying to open incorrect file path.
        string filePath = HttpContext.Current.Server.MapPath("SampleExcel.xlsx");
        FileStream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read);
        importRequest.FileStream = fileStream;
 
        string spreadsheetData = Spreadsheet.Open(importRequest);
        fileStream.Close();
        context.Response.Write(spreadsheetData);               
    }
    catch (Exception ex)
    {
        context.Response.Write("File not found.");
    }
}

 

The below screenshot represents the output of the above code example.

spreadsheet error message sample preview

Figure: Spreadsheet with custom alert message

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