A bit of file management is required on the Server so as to be able to delete uploaded and on server generated zip files. This post covers selecting working folders to be cleared and actioning that.

There is a client page called Management on which there is a link to the previously covered Upload command for uploading a zip file of samples. There are other commands on that page. This post is focused upon the three Clear commands.

  • Upload a Samples zip file to Server, and extract.
    • Or …Select a previously uploaded Samples zip.
  • Switch to Samples folder on Server and re-get app data from Server
    • ServerSamplesFolder in appsettings.json in Server code.
    • Assumes samples are already there. Use above commands.
  • Switch to Dev folder and re-get app data from Server.
    • See DevPathToRepository in appsettings.json in Server code.
    • Assumes there are samples there.
  • Clear created (and downloaded) Project Zip Files on Server.
    • Zip files are created and saved on Server before Download.
    • Nb: ZipFolder is specified in appsettings.json on server.
  • Clear Uploaded Files (Samples zips) on Server.
    • A Samples folder, as zip file, is uploaded to here then extracted.
  • Clear Samples folder on Server.
    • Zip files on upload are extracted to here.

The Management Client Page Commands

. Nb: For remote deployment, these commands would require authentication.

Client Code for Http Delete

This just sends a parameterized Http Delete command to the Server.
The parameter is a numerical code specifying which folder is to be cleared.

    public async Task<string> Delete (string folder)
    {
        // For Ids see Server Properties/Resources File
        int id=-1;
        switch (folder)
        {
            case "uploads": //Nb: Zips uploaded (Zipped Sample folders)
                id = 2;
                break;
            case "downloads": //Nb: Zips downloaded from zipped up project/solution folders
                id = 3;
                break;
            case "samples":  // Where  uploads are extracted to.
                id = 1;
                break;
        }
        if (id != -1)
        {
            string fileContents = "";
            var strn = await client.DeleteAsync(ServiceEndpoint + $"/{id}");
            fileContents = await strn.Content.ReadAsStringAsync();
            return fileContents;
        }
        else
        {
            return "Not a valid folder on server.";
        }
    }

The Server Response

The Server Delete Handler is embedded in the same Controller as the Get Handlers, namely Samples. Hence the ServiceEndpoint used by the client in client.DeleteAsync() as above:

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

The Samples Controller Http Delete Handler prototype:

    [Route("api/[controller]")]
    [ApiController]
    public class SamplesController : ControllerBase
    {
         [HttpDelete("{FolderId}")]
        public async Task Delete(string FolderId)
        {
        }
    }

The following folders are defined in the server code:

  • ServerZipFolder
  • ServerUploadsFolder
  • ServerSamplesFolder

Folder clearing is done by deletion then recreation.

    [HttpDelete("{FolderId}")]
    public async Task Delete(string FolderId)
    {
        string text = "";
        string folder = "skip";
        int ResponseStatus = 200; //OK
        try
        {
            switch (FolderId)
            {
                case "3":
                    folder = ServerZipFolder;
                    break;
                case "2":
                    folder = ServerUploadsFolder;
                    break;
                case "1":
                    folder = ServerSamplesFolder;
                    break;
            }
            if (folder != "skip")
            {
                if (Directory.Exists(folder))
                {
                    Directory.Delete(folder,true);
                }
                Directory.CreateDirectory(folder);
                text = "OK";
            }
            else
            {
                ResponseStatus = 404;
                text = "Id doesn't correspond to a folder (Valid Ids:1..3)";
            }
        } catch (Exception ex)
        {
            Response.StatusCode = 500;
            text = ex.Message;
        }
        Response.StatusCode = ResponseStatus;
        await Response.WriteAsync(text);
    }

The Other Commands

The Management Razor page can be viewed here on Github

The Client Service can be viewed here on GitHub

The Service Controller can be viewed here on GitHub


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