Trabajando con GridJs del lado del servidor
Trabajando con GridJs del lado del servidor
1. agregar servicio en ConfigureServices en startup.cs
services.AddScoped<IGridJsService, GridJsService>();
2. Establecer la ruta para almacenar archivos en caché
Puedes elegir cualquiera de las siguientes formas:
Opción 1: Establecer GridJsOptions en ConfigureServices en Startup.cs
services.Configure<GridJsOptions>(options =>
{
options.FileCacheDirectory = TestConfig.TempDir;
});
Opción 2: Establecer directamente la propiedad estática
Config.FileCacheDirectory = TestConfig.TempDir;
Opción 3: Definir tu propia regla de almacenamiento en caché implementando GridCacheForStream
Para el almacenamiento de archivos local, aquí hay un ejemplo:
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; | |
} | |
} |
Para almacenamiento en el lado del servidor, también proporcionamos un ejemplo. Por favor, revisa:
https://github.com/aspose-cells/Aspose.Cells-for-.NET/blob/master/Examples_GridJs/Models/AwsCache.cs
Para más detalles sobre almacenamiento, consulta esta guía
3. Implementar acciones en el controlador
1. Crear un controlador que extienda GridJsControllerBase
public class GridJs2Controller : GridJsControllerBase
2. Obtener JSON en la acción
Hay dos formas de obtener datos JSON en tu acción:
Opción 1: Usando GridJsWorkbook
GridJsWorkbook wbj = new GridJsWorkbook();
Workbook wb = new Workbook(fullFilePath);
wbj.ImportExcelFile(wb);
String ret =wbj.ExportToJson(fileName);
Opción 2: Usando IGridJsService en GridJsControllerBase
String uid= GridJsWorkbook.GetUidForFile(fileName)
StringBuilder ret= _gridJsService.DetailFileJsonWithUid(fullFilePath, uid);
Para obtener información detallada, puede consultar el ejemplo aquí: https://github.com/aspose-cells/Aspose.Cells-for-.NET/tree/master/Examples_GridJs