-1

I want to know how can i prompt user to download pdf file.

I have below code but it is not returning anything.

public ActionResult DownloadAssetClassGuide()
{
    string folder = @"C:\NewFolder";
    string file = "xyz.pdf";

    string fullPath = Path.Combine(folder, file);

    byte[] fileBytes = System.IO.File.ReadAllBytes(fullPath);

    return File(fileBytes, "application/pdf", file);
}

am i missing something?

Rockstar
  • 59
  • 2
  • 9

1 Answers1

0

Any path to a file or a folder should first got mapped to the server because you are using a virtual path. Try to map the path to the server first using HttpContext.Current.Server.MapPath:

FileInfo fInfo = new FileInfo(Server.MapPath(fullPath));
FileStream fileStream = new FileStream(fullPath, FileMode.Open, FileAccess.Read);
BinaryReader reader = new BinaryReader(fileStream);
byte[] fileBytes = reader.ReadBytes((int)fInfo.Length);

This method worked with me when I face same issue

Ibrahim Amer
  • 1,147
  • 4
  • 19
  • 46