簡體   English   中英

Asp.Net MVC下載格式未知的文件

[英]Asp.Net MVC downloading a file with unknown format

我想在我的MVC控制器中下載一個格式未知的文件。

這是我的代碼

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();

        }

    }

但它沒有工作,但沒有給出任何錯誤。

我該如何解決這個問題?

要將文件返回到客戶端,您必須返回如下所示的ContentResult,

    public ActionResult Download()
    {
        return File(@"<filepath>", "application/octet-stream","<downloadFileName.ext>");
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM