Search code examples
c#stringtrim

Trim the URL and fetch the name after the last Slash in c#


I need to Trim the URL that I am fetching from the Database and need to show the value after the last Slash(/).

Tried using Trim Function.

public ActionResult DownloadFile(Int64 NurseId, Int64 PostedJobId, Int64 DocumentId)
    {
        NurseDAL objNurseDAL = new NurseDAL();

        Result objResult = objNurseDAL.FetchDocumentURLfromDocID(DocumentId);


        string path = "D:/TFSProjects/Dot Net Project/NurseOneStop.WebSite/NurseOneStop.WebSite/";
        byte[] fileBytes = System.IO.File.ReadAllBytes(path + objResult.Results.DocumentUrl);
        var URL = objResult.Results.DocumentUrl; //(/Content/Images/UploadedDocuments/20190205131053233.pdf)

        string fileName = filename.extension;
        return File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, fileName);
    }

The expected output should be 20190205131053233.pdf from the string URL: /Content/Images/UploadedDocuments/20190205131053233.pdf


Solution

  • Trim can be used only to trim specific characters from a specified string. For this scenario, you can use Path.GetFileName(fileName). You will have to refer System.IO by adding a using statement