0

i want to show an exciting file from my db into the details page

and i have this problem that the file saved as a long path like this :

D:\Projects\asp\Portail_Candida\Portail_Candida\CandidatCvs\ayman_jarmoune_cv_fr.pdf

and when i wanna see it it doesn't shows,

the only way that shows that it needs to be "~/CandidatCvs/as.pdf"

here's my code :

this is the save methode :

 public ActionResult Upload(Candidateur candida)
    {
        using (RecrutementPortailEntities entity = new RecrutementPortailEntities())
        {
            var candidate = new candidature()
            {
                NomCandidature = candida.NomCandidature,
                PrenomCandidature = candida.PrenomCandidature,
                MailCandidature = candida.MailCandidature,
                TeleCandidature = candida.TeleCandidature,
                NiveauEtudeCandidature = candida.NiveauEtudeCandidature,
                CvCandidature = SaveToPhysicalLocation(candida.CvCandidature),
                NumbMoisExperienceCandidature = candida.NumbMoisExperienceCandidature,
                DatedepositCandidature = DateTime.Now,
                DernierEmployeeCandidature = candida.DernierEmployeeCandidature
            };
            entity.candidatures.Add(candidate);
            entity.SaveChanges();
        }
        return View(candida);
    }


    private string SaveToPhysicalLocation(HttpPostedFileBase file)
    {
        if (file.ContentLength > 0)
        {
            var fileName = Path.GetFileName(file.FileName);
            var path = Path.Combine(Server.MapPath("\\CandidatCvs"), fileName);
            file.SaveAs(path);
            return path;
        }
        return string.Empty;
    }

and here's the cshtml :

            <div>

            <embed  src="@Model.CvCandidature" type="application/pdf" height="700" width="700">


        </div>

1 Answers1

1

You have 2 options here:

1- Create a Action inside the Controller that return a byte[] (that's your file) and in src tag in your embed, set the action: /File/GetFile -> Controller File, Action: GetFile. Code:

public Task<IActionResult> GetFile()
{
  byte[] file = File.ReadAll("pathHere");
  return File(file, "application/pdf");
}

2-You can convert your pdf in base64 content and in embed tag set the base64: MIME encoding of a PDF file in an HTML page

Rafael Rocha
  • 174
  • 14