user3052186
user3052186

Reputation: 1

Asp.Net MVC downloading a file with unknown format

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?

Upvotes: 0

Views: 1315

Answers (1)

Sravan
Sravan

Reputation: 1135

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>");
    }

Upvotes: 2

Related Questions