[英]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.