-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?

2
  • You need to specify the Content-Disposition header. For more information see the post I marked this as a duplicate of. Commented Sep 19, 2015 at 0:38
  • How are you sending the user to the action method? Commented Sep 19, 2015 at 0:53

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.