繁体   English   中英

System.IO.FileNotFoundException: 在 asp.net MVC 中“找不到文件”

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

我尝试生成 pdf 文件。我的数据正确传递给内容。然后我使用 convertStringToPDF() 方法将该字符串转换为 pd。然后我尝试使用 server.MapPath() 方法将其下载到我的项目文件位置。但它给出我下面的错误。我的主要目标是将该pdf下载到给定的项目文件夹位置

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

这是我的方法:

[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不是字符串。 File()返回一个FileResult ,该对象的目的是将字节流式传输到浏览器。

您还没有定义lg是什么类型,也没有定义convertStringToPDF作用。 它是否返回 pdf 内容流? 在这种情况下,您根本不需要引用文件系统;

        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");
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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