簡體   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