Working with GridJs Server Side
Working with GridJs Server Side
1. add service in ConfigureServices in startup.cs
services.AddScoped<IGridJsService, GridJsService>();
2. Set the Path to Store Cache Files
You can choose any of the following ways:
Option 1: Set GridJsOptions in ConfigureServices in Startup.cs
services.Configure<GridJsOptions>(options =>
{
options.FileCacheDirectory = TestConfig.TempDir;
});
Option 2: Directly Set the Static Property
Config.FileCacheDirectory = TestConfig.TempDir;
Option 3: Define Your Own Cache Storage Rule by Implementing GridCacheForStream
For local file storage ,here is an example:
| public class LocalFileCache : GridCacheForStream | |
| { | |
| public LocalFileCache() | |
| { | |
| string streampath = Path.Combine(Config.FileCacheDirectory, "streamcache"); | |
| if (!Directory.Exists(streampath)) | |
| { | |
| //create cache directory if not exists | |
| Directory.CreateDirectory(streampath); | |
| } | |
| } | |
| /// <summary> | |
| /// Implement this method to savecache,save the stream to the cache object with the key id. | |
| /// </summary> | |
| /// <param name="s">the source stream </param> | |
| /// <param name="uid">he key id.</param> | |
| public override void SaveStream(Stream s, String uid) | |
| { | |
| String filepath = Path.Combine(Config.FileCacheDirectory + Path.DirectorySeparatorChar + "streamcache", uid.Replace('/', '.')); | |
| using (FileStream fs = new FileStream(filepath, FileMode.Create)) | |
| { | |
| s.Position = 0; | |
| s.CopyTo(fs); | |
| } | |
| } | |
| /// <summary> | |
| /// Implement this method to loadcache with the key uid,return the stream from the cache object. | |
| /// </summary> | |
| /// <param name="uid">the key id</param> | |
| /// <returns>the stream from the cache</returns> | |
| public override Stream LoadStream(String uid) | |
| { | |
| String filepath = Path.Combine(Config.FileCacheDirectory + Path.DirectorySeparatorChar + "streamcache", uid.Replace('/', '.')); | |
| FileStream fs = new FileStream(filepath, FileMode.Open); | |
| return fs; | |
| } | |
| /// <summary> | |
| /// implement the url in action controller to get the file | |
| /// </summary> | |
| /// <param name="uid">the key id</param> | |
| /// <returns></returns> | |
| public override String GetFileUrl(string uid) | |
| { | |
| return "/GridJs2/GetFile?id=" + uid; | |
| } | |
| } |
For server-side storage,we also provide an example.Please check:
For more details about storage, please refer to this guide
3. Implement Controller Actions
1. Create a Controller that Extends GridJsControllerBase
public class GridJs2Controller : GridJsControllerBase
2 Get JSON in action
There are two ways to get JSON data in your action:
Option 1: Using GridJsWorkbook
GridJsWorkbook wbj = new GridJsWorkbook();
Workbook wb = new Workbook(fullFilePath);
wbj.ImportExcelFile(wb);
String ret =wbj.ExportToJson(fileName);
Option 2: Using IGridJsService in GridJsControllerBase
String uid= GridJsWorkbook.GetUidForFile(fileName)
StringBuilder ret= _gridJsService.DetailFileJsonWithUid(fullFilePath, uid);
For detail info ,you can check the example here: https://github.com/aspose-cells/Aspose.Cells.Grid-for-.NET/tree/master/Examples_GridJs