Arbeiten mit GridJs Server Side
Arbeiten mit GridJs Server Side
1. Dienst in ConfigureServices in startup.cs hinzufügen
services.AddScoped<IGridJsService, GridJsService>();
2. Pfad zum Speichern von Cache-Dateien festlegen
Sie können eine der folgenden Methoden wählen:
Option 1: Setzen Sie GridJsOptions in ConfigureServices in Startup.cs
services.Configure<GridJsOptions>(options =>
{
options.FileCacheDirectory = TestConfig.TempDir;
});
Option 2: Setzen Sie die statische Eigenschaft direkt
Config.FileCacheDirectory = TestConfig.TempDir;
Option 3: Definieren Sie Ihre eigene Cache-Speicherregel durch Implementierung von GridCacheForStream
Für die lokale Dateispeicherung, hier ist ein Beispiel:
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; | |
} | |
} |
Für die serverseitige Speicherung stellen wir auch ein Beispiel bereit. Bitte prüfen Sie:
https://github.com/aspose-cells/Aspose.Cells-for-.NET/blob/master/Examples_GridJs/Models/AwsCache.cs
Für weitere Details zur Speicherung lesen Sie bitte diesen Leitfaden
3. Implementieren Sie Controller-Aktionen
1. Erstellen Sie einen Controller, der GridJsControllerBase erweitert
public class GridJs2Controller : GridJsControllerBase
2. JSON in Aktion abrufen
Es gibt zwei Möglichkeiten, JSON-Daten in Ihrer Aktion zu erhalten:
Option 1: Mit GridJsWorkbook
GridJsWorkbook wbj = new GridJsWorkbook();
Workbook wb = new Workbook(fullFilePath);
wbj.ImportExcelFile(wb);
String ret =wbj.ExportToJson(fileName);
Option 2: Mit IGridJsService in GridJsControllerBase
String uid= GridJsWorkbook.GetUidForFile(fileName)
StringBuilder ret= _gridJsService.DetailFileJsonWithUid(fullFilePath, uid);
Für weitere Details können Sie hier das Beispiel überprüfen: https://github.com/aspose-cells/Aspose.Cells-for-.NET/tree/master/Examples_GridJs