简体   繁体   中英

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM