The Server response to a Client Http Get request for a Text, Markdown and Image files.

Client Request Commands

The client command format is

    Command~<Filename>~<FolderId>

Note that it is a tilde separated list.

See here for a list of commands. These are sent as an Http Request parameter and decoded as below.

Server Response

The Server Get Handlers are embedded in a Controller called Samples, hence the ServiceEndpoint used by the client in its Http Get calls to the server:

    private const string ServiceEndpoint = "/api/Samples";

The Samples Controller Http Get Handler prototype:

    [Route("api/[controller]")]
    [ApiController]
    public class SamplesController : ControllerBase
    {
        [HttpGet]
        public string Get()
        {
            // Return scanned data
        }

        // Parameterized Get commmands
        [HttpGet("{param}")]
        public string Get(string param)
        {
            // param is of the format: Command~<Filename>~<FolderId>
        }
    }

For Text and Markdown commands, the text is read from the file on the server as specified by the FolderId and Filename and returned to the client. For an Image command, the file contents is loaded into Bitmap in memory then transformed into a Base64 String and returned.

The code that follows is within the parameterized HttpGet.

Get file path from the command:

    string[] params = param.Split(new char[] { '~' });
    string Cmd = params[0]; // Markdown ,Text, Image or Zip
    string Filename = params[1]
    string FolderId = params[2]
    int foldId = int.Parse(folderId);
    var fld = from f in FolderTree.AllFolderTrees where f.Id == foldId select f;
    folder = fld.First();
    fpath = folder.FolderPath;
    path = Path.Combine(fpath, FileName);

Get Text or Markdown File contents.

    string text="";
    text = System.IO.File.ReadAllText(path);
    return text;

Get Image File Contents

… transform to Base64String.

    string text="";
    using (Bitmap ImageContents = new Bitmap(path))
    {
        var imgInput = System.Drawing.Image.FromFile(path);
        var thisFormat = imgInput.RawFormat;
        ImageContents.Save(ms, thisFormat);
        text = "data:image/png;base64," + Convert.ToBase64String(ms.ToArray());
    }
    return text;

Note: This is simplified code. There are checks and balances in the actual code. See the repository on GitHub for the actual code: djaus2/GetListofSamples


 TopicSubtopic
  Next: >  
   
 This Category Links 
Category:Blazor Index:Blazor
  Next: > Blazor How To
<  Prev:   Blazor How To