How to create an open service for a JavaScript Spreadsheet in a Web Forms application?
This knowledge base article explains how to create an open service for a JavaScript Spreadsheet in a WebForms application. To achieve this, you need to create a web service with the following NuGet packages installed:
• Syncfusion.EJ2.Spreadsheet
• Syncfusion.XlsIO.AspNet.Mvc5
• Syncfusion.ExcelToPdfConverter.AspNet.Mvc5
• Syncfusion.Pdf.AspNet.Mvc5
• Syncfusion.ExcelChartToImageConverter.AspNet.Mvc5
• Syncfusion.EJ2.MVC5
Next, create an HttpPostedFileWrapper based on the HTTP request file and set the stream’s position to 0 to ensure that the file is read properly.
Afterward, create an open request and pass it to the Workbook.Open() method to ensure the file opens correctly in the WebForms application using the web service.
[WebService1.asmx.cs]:
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Pipes;
using System.Linq;
using System.Net.Mime;
using System.Web;
using System.Web.Script.Services;
using System.Web.Services;
using Syncfusion.EJ2.Spreadsheet;
using Syncfusion.XlsIO;
using static System.Net.Mime.MediaTypeNames;
namespace WebApplication1
{
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line.
// [System.Web.Script.Services.ScriptService]
public class WebService1 : System.Web.Services.WebService
{
[WebMethod]
public string HelloWorld()
{
return "Hello World";
}
[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);
}
}
}
Sample Link: https://www.syncfusion.com/downloads/support/directtrac/general/ze/WebApplication1_(2)140380233
Output: