Reputation: 41
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
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);
}
Upvotes: 1
Views: 2326
Reputation: 11100
var filename
is not a string. File()
returns a FileResult
, an object that's intended purpose is to stream bytes to the browser.
You haven't defined what type lg
is, nor what convertStringToPDF
does. Does it return a stream of the pdf content? 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");
}
Upvotes: 1