Search code examples
c#kendo-uistreamasp.net-mvc-5.1

how to save a pdf to folder in root directory and return it to client


I am using kendo-ui chart export to pdf function. i am using a MVC controller to return it to the client, this works fine. I would like to also save the pdf into a folder as well. can i do both at the same time?

 [HttpPost]
    public ActionResult Save(string contentType, string base64)
    {
        var fileContents = Convert.FromBase64String(base64);


        var dt = DateTime.Now.ToString("d").Replace('/', '-').Replace(':', '-');
        var fileName = string.Format("{0}--{1}.pdf", "SubjectProperty", dt);
        string path = Path.Combine(Server.MapPath("~/Pdfs/"), Path.GetFileName(fileName));

        return File(fileContents, contentType, fileName);

    }

Solution

  • Just write the file contents to the file path that you generated, just before the return statement:

    File.WriteAllBytes(path,fileContents);