Can t load excel file from ASP NET 4.6.2 WebService - file not supported


Hello, 

I need use a SpreadSheet control into a customer's application. 

I just need a confirmation that Syncfusion (26.2.4) can be implemented in this application before to buy a licence for 2 years. 

It's an application working on WebForms ASP.NET C# 4.6.2. I created a WebService for that.

I can't load excel file in spreadsheet grid. Just a message : file not supported

application.IsSupported(stream); => OK

var result = Workbook.Open(open);  => NOT OK : Open method can't parse excel file. (Excel checked before)

I've same issue with XLS and CSV files

My project include nuget's references :

Image_7945_1724267608838

with Newtonsoft.Json v13.0.3

See code for tests below (very simple) :

JS Code :

            const spreadsheet = new ej.spreadsheet.Spreadsheet({

                allowOpen: true,

                openUrl: '/Pilote/Services/Upload.asmx/Open',

                openComplete: function () {

                    console.log('openComplete', spreadsheet);

                },

                openFailure: function (args) {

                    console.log('openFailure', args);

                },

            });

            spreadsheet.appendTo('#spreadsheet');

C# Code WebService :

    public class Upload : System.Web.Services.WebService

    {

        [WebMethod]

        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]

        public void Open()

        {

            string result = "";


            var files = HttpContext.Current.Request.Files;

            try

            {

                var postedFile = files.Get(0);

                var stream = postedFile.InputStream;


                using (ExcelEngine excelEngine = new ExcelEngine())

                {

                    IApplication application = excelEngine.Excel;

                    application.DefaultVersion = ExcelVersion.Xlsx;

                    if (!application.IsSupported(stream))

                    {

                        return;

                    }

                }


                var memoryFile = new MemoryFile(stream, postedFile.ContentType, postedFile.FileName);

                var postedFiles = new List<HttpPostedFileBase>();

                postedFiles.Add((HttpPostedFileBase)memoryFile);


                var open = new OpenRequest();

                open.File = postedFiles.ToArray();

                result = Workbook.Open(open);


                stream.Close();

                stream.Dispose();

            }

            catch (Exception ex)

            {

                PiloteTrace.Instance.AddLogError(ex);

            }


            Context.Response.Write(result);

        }


Best regards


7 Replies

RC REILER Cyril [EIFFAGE ENERGIE SYSTEMES] August 22, 2024 01:41 PM UTC

Class missing to debug this issue


 class MemoryFile : HttpPostedFileBase

    {

        readonly Stream stream;

        readonly string contentType;

        readonly string fileName;


        public MemoryFile(Stream stream, string contentType, string fileName)

        {

            this.stream = stream;

            this.contentType = contentType;

            this.fileName = fileName;

        }


        public override int ContentLength

        {

            get { return (int)stream.Length; }

        }


        public override string ContentType

        {

            get { return contentType; }

        }


        public override string FileName

        {

            get { return fileName; }

        }


        public override Stream InputStream

        {

            get { return stream; }

        }


        public override void SaveAs(string filename)

        {

            using (var file = File.Open(filename, FileMode.CreateNew)) {

                stream.CopyTo(file);

            }

        }

    }



BP Babu Periyasamy Syncfusion Team August 22, 2024 03:17 PM UTC

Hi Cyril REILER,


We have checked your reported query based on your shared details and code snippet. And we would like to let you know, currently Spreadsheet component don’t have support for import and export services in ASP.NET Web Form and we do only support for the ASP .NET Core and ASP .NET MVC platforms.


So, we suggest you to create your own local service in ASP Core/MVC and use the hosted URL in the Open and Save URL in your end to perform import and export actions. For your reference, we have prepared the local service with the open and save controller codes and attached below along with the details for your reference,


Please follow the below steps to launch the local service on your end.

  1. Download and unzip the attached local service (ASP.NET Core WebAPI).
  2. Open the WebAPI.sln file in the WebAPI folder.
  3. In that right click on the Dependencies folder inside WebAPI.
  4. Then, click the Manage Nuget Packages.
  5. In Browse, search Syncfusion.EJ2.Spreadsheet.AspNet.Core package and install the latest package.
  6. If already, the package exists, remove the package, and re-install it to get the latest package with its necessary dependent packages will get downloaded.
  7. Now, build the solution and run in a local host.


For your convenience, we have attached the open and save code snippets in local service and sample below.

Code snippet:


//Open method

[HttpPost]

[Route("Open")]

public IActionResult Open([FromForm]IFormCollection openRequest)

{

    OpenRequest open = new OpenRequest();

    open.File = openRequest.Files[0];

    return Content(Workbook.Open(open));

}

//Save method

[HttpPost]

[Route("Save")]

public IActionResult Save([FromForm]SaveSettings saveSettings)

{

    return Workbook.Save(saveSettings);

}


Web Service samplehttps://www.syncfusion.com/downloads/support/directtrac/general/ze/WebAPI_(2)1082676101


After, launching the local service, you need to update the open and save URL in the Client-Side sample like in the below code snippet,


openUrl= 'https://localhost:{port number}/api/spreadsheet/open'

saveUrl= 'https://localhost:{port number}/api/spreadsheet/save'

Example:

openUrl'https://localhost:44354/api/spreadsheet/open'

saveUrl'https://localhost:44354/api/spreadsheet/save'


For more information regarding the Open/Save in Spreadsheet, please refer the below documentation,


Documentation link
https://ej2.syncfusion.com/javascript/documentation/spreadsheet/open-save

KB linkhttps://www.syncfusion.com/kb/13200/how-to-remove-trial-version-tab-created-in-spreadsheet

Local service available in below GitHub location also
:

https://github.com/SyncfusionExamples/EJ2-Spreadsheet-WebServices/

Alternatively, we can also host and run our Spreadsheet-oriented service as a Docker image. Utilizing a Docker image necessitates only a simple and basic Docker environment with minimal commands to effortlessly host and run our service.

You can utilize the below service and use it for Spreadsheet Open and Save services, by pulling the spreadsheet docker image and following the steps from the URL mentioned below.

Spreadsheet docker image: https://hub.docker.com/r/syncfusion/spreadsheet-server

For more information regarding the docker deployment, please refer the below documentation,

https://ej2.syncfusion.com/javascript/documentation/spreadsheet/docker-deployment


Kindly, check the above informations and get back to us for further clarifications.



RC REILER Cyril [EIFFAGE ENERGIE SYSTEMES] August 22, 2024 05:19 PM UTC

Hello, 

Sorry but Spreadsheet [ Javascript ] control must be compatible with webform because DocumentEditor control is compatible. 

We can create page with a spreadsheet on jQuery 3.7 and create excel document with POST request on WebForm WebService.

The issue concern "Syncfusion.EJ2.Spreadsheet" API backend side. API is compatible .NET 4.5. We must just install Newtonsoft.Json 13 minimum (exception after call to OpenRequest method if it's not installed)

It's an issue with JSON parsing.

We are testing DocumentEditor Javascript Editor. We can edit DocX document into WebForm project very simply.

DocumentEditor Javascript Control and API can be used into WebForms Project because API is compatible .NET 4.5 minimum. (See screenshot below). 

No problem with WebForm or MVC technology with this API. So I should be possible to use SpreadSheet API too. No differences for us. 

Image_5984_1724346341452

It's very strange that we can use DocumentEditor Control and not SpreadSheet Control !!



RC REILER Cyril [EIFFAGE ENERGIE SYSTEMES] August 22, 2024 07:54 PM UTC

Hello, 

Do you know if it's possible for us to modify API SpreadSheet source code after subscribe licence ?

It's very important to resolve this issue for customer's application with 2 years of maintenance. 

Best regards



BP Babu Periyasamy Syncfusion Team August 23, 2024 01:23 PM UTC

Hi Cyril REILER,


Sorry for the inconvenience caused.


Previously, we have tried the webservice using the `EJ2.Spreadsheet.Asp.Net Core` and because of that we are not able to achieve the webservice implementation in  Web Forms as we have faced issues on `Workbook.open()` method to accept the request.


Now, based on your screenshot we have tried preparing the Web service in Web Forms using the MVC package. And now we are able to pass the file request to the `Workbook.open()` method, but we are getting the result as `Unsupported File` like the below screenshot,



Therefore, we need to check this issue by connecting our source and we need some additional time to validate in our end. We will check the issue by connecting to our source and update you the further details as soon as possible. We appreciate your patience until then.


However, we have attached the application that we have tried in our end below for your reference,


Web Forms application: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication12065752019


Please find the required NuGet details for creating the webservice below:


  • Syncfusion.EJ2.Spreadsheet
  • Syncfusion.XlsIO.AspNet.Mvc5
  • Syncfusion.ExcelToPdfConverter.AspNet.Mvc5
  • Syncfusion.Pdf.AspNet.Mvc5
  • Syncfusion.ExcelChartToImageConverter.AspNet.Mvc5
  • Syncfusion.EJ2.MVC5





RC REILER Cyril [EIFFAGE ENERGIE SYSTEMES] August 26, 2024 12:01 PM UTC

Hello,

We confirm you that we are same message with Web Forms application in last post

We can wait resolving of this issue until end of september 

Thank you

Best regards



BP Babu Periyasamy Syncfusion Team August 27, 2024 08:19 AM UTC

Hi Cyril REILER,


Thanks for your update.


Now, we have tried the Webservice in Web Forms as like the same handling for MVC web service. And I have modified the Webservice code like the below in the WebForms application,


[WebMethod]

 

[ScriptMethod(ResponseFormat = ResponseFormat.Json)]

 

public void Open()

 

{

    string result = "";

    var files = HttpContext.Current.Request.Files;

    try

    {

        var postedFile = files.Get(0);

        HttpPostedFileWrapper fileWrapper = new HttpPostedFileWrapper(postedFile);

        fileWrapper.InputStream.Position = 0;

        HttpPostedFileBase[] newfiles = new HttpPostedFileBase[1];

        newfiles[0] = fileWrapper;

        OpenRequest open = new OpenRequest();

        open.File = newfiles;

        result = Workbook.Open(open);

 

    }

    catch (Exception ex)

    {

        return;

    }

    Context.Response.Write(result);

 

}


In the above code, I have created `HttpPostedFileWrapper` based on the Http request file and then I have set the position of the stream to `0` to read the file properly. And then, I have created the openRequest and pass the request to the `Workbook.Open(open)` method. Now, the file is properly opened in the created Web Form application using the Web service in it.


Additional to the Nuget packages that we have shared in our update, we need to install the `Syncfusion.EJ2.MVC5` also. For your convenience, we have prepared the video demonstration and attached below along with the prepared Web Form application for your reference,


Web Form application: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_(2)140380233


Video demonstration: https://www.syncfusion.com/downloads/support/directtrac/general/ze/Open_File_in_WebForms-798682669


Kindly, check the above details and get back to us for further clarifications.


Loader.
Up arrow icon