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



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

System.IO.FileNotFoundException: 'Could not find file 'C:_ProjectsxxxxxxxxRepositoryVaultSystem.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");
}

相关内容

最新更新