0

I want to download a file with an unknown format in my MVC controller.

Here is my code

public static void DownloadAttachment(string Name,string physicalPath ) {

        if (System.IO.File.Exists(physicalPath))
        {
            System.IO.FileInfo file = new System.IO.FileInfo(physicalPath);
            byte[] fileB = File.ReadAllBytes(physicalPath);
            HttpContext context = HttpContext.Current;
            context.Response.ClearHeaders();
            context.Response.Clear();
            context.Response.AddHeader("content-disposition", "attachment;                  filename="+Name+";");
            context.Response.ContentType = "application/force-download";
            context.Response.BinaryWrite(fileB.ToArray());
            context.Response.Flush();
            context.Response.End();

        }

    }

But it is not working but doesn't give any error.

How do I solve this problem?

1
  • Does it work with ContentType of "application/octet-stream"? Commented Nov 30, 2013 at 14:10

1 Answer 1

2

To return a file into the Client you have to return a ContentResult like below,

    public ActionResult Download()
    {
        return File(@"<filepath>", "application/octet-stream","<downloadFileName.ext>");
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.