简体   繁体   English

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

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

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. var filename不是字符串。 File() returns a FileResult , an object that's intended purpose is to stream bytes to the browser. File()返回一个FileResult ,该对象的目的是将字节流式传输到浏览器。

You haven't defined what type lg is, nor what convertStringToPDF does.您还没有定义lg是什么类型,也没有定义convertStringToPDF作用。 Does it return a stream of the pdf content?它是否返回 pdf 内容流? 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");
        }

暂无
暂无

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

相关问题 System.IO.FileNotFoundException:找不到文件 - System.IO.FileNotFoundException : Could not find file ASP零:System.IO.FileNotFoundException:找不到文件'c:\\ windows \\ system32 \\ inetsrv \\ log4net.config' - ASP Zero :System.IO.FileNotFoundException: Could not find file 'c:\windows\system32\inetsrv\log4net.config' Xamarin Android System.IO.FileNotFoundException:找不到文件异常 - Xamarin Android System.IO.FileNotFoundException: Could not find file Exception System.IO.FileNotFoundException: '找不到文件 Xamarin Forms - System.IO.FileNotFoundException: 'Could not find file Xamarin Forms 控制台应用程序:System.IO.FileNotFoundException:找不到文件 - Console Application: System.IO.FileNotFoundException: Could not find file System.IO.FileNotFoundException - 系统找不到指定的路径? - System.IO.FileNotFoundException - the system could not find the path specified? System.IO.FileNotFoundException:无法加载文件或程序集'System.Web.DataVisualization MEF MVC - System.IO.FileNotFoundException: Could not load file or assembly 'System.Web.DataVisualization MEF MVC System.IO.FileNotFoundException:无法加载文件或程序集。 系统找不到文件SharpSvn.dll - System.IO.FileNotFoundException: Could not load file or assembly. The system cannot find the file SharpSvn.dll System.IO.FileNotFoundException:无法加载指定的文件。 文件名:“ System.Net.Sockets.resources” - System.IO.FileNotFoundException: Could not load the specified file. File name: 'System.Net.Sockets.resources' .NET 6 System.IO.FileNotFoundException:无法加载文件或程序集 System.Linq,版本 = 6.0.0.0 - .NET 6 System.IO.FileNotFoundException: Could not load file or assembly System.Linq, Version=6.0.0.0
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM