简体   繁体   中英

System.IO.FileNotFoundException: 'Could not find file ' in asp.net MVC

I tried to generate pdf file.My data correctly pass to the content.Then i Convert that string to pd using convertStringToPDF() method.Then I tried to download it to my project file location using server.MapPath() method.But it gives me below error.My main aim is download that pdf to given project folder location

System.IO.FileNotFoundException: 'Could not find file 'C:\_Projects\xxxx\xxxx\Repository\Vault\System.Web.Mvc.FileContentResult'.'

That is my method:

[HttpPost]
        public void SendPdf(long groupId)
        {
            var content = string.Empty;
            var writer = new StringWriter();
            string dir = Server.MapPath("~/Repository/Vault");

            var loggedUser = User.Identity.Name;
            ViewData.Model = lg.GetGCSessionsByGroupID(groupId, loggedUser);
            var view = ViewEngines.Engines.FindView(ControllerContext, "GCSessiontablePartial", null);
            var context = new ViewContext(ControllerContext, view.View, ViewData, TempData, writer);
            view.View.Render(context, writer);
            writer.Flush();


            content = writer.ToString();
            var filename = File(lg.convertStringToPDF(content), "application/pdf", "reportcard.pdf");
            byte[] fileBytes = System.IO.File.ReadAllBytes(dir + @"\" + filename);//This line gives me error

           // var k = File(fileBytes, System.Net.Mime.MediaTypeNames.Application.Octet, filename);

        }

var filename is not a string. File() returns a FileResult , an object that's intended purpose is to stream bytes to the browser.

You haven't defined what type lg is, nor what convertStringToPDF does. Does it return a stream of the pdf content? In which case you wouldn't need to reference the file system at all;

        public IActionResult SendPdf(long groupId)
        {
            var writer = new StringWriter();

            var loggedUser = User.Identity.Name;
            ViewData.Model = lg.GetGCSessionsByGroupID(groupId, loggedUser);
            var view = ViewEngines.Engines.FindView(ControllerContext, "GCSessiontablePartial", null);
            var context = new ViewContext(ControllerContext, view.View, ViewData, TempData, writer);
            view.View.Render(context, writer);
            writer.Flush();

            var content = writer.ToString();
            return File(lg.convertStringToPDF(content), "application/pdf", "reportcard.pdf");
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM