MVC下载文件方式

MVC下载文件方式

方式一:

public FileStreamResult DownFile(string filePath, string fileName)         {       string absoluFilePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] +      filePath);        return File(new FileStream(absoluFilePath, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));         }

 

 

 

方式二:

 

public ActionResult DownFile(string filePath, string fileName)         {             filePath = Server.MapPath(System.Configuration.ConfigurationManager.AppSettings["AttachmentPath"] + filePath);             FileStream fs = new FileStream(filePath, FileMode.Open);             byte[] bytes = new byte[(int)fs.Length];             fs.Read(bytes, 0, bytes.Length);             fs.Close();             Response.Charset = "UTF-8";             Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8");             Response.ContentType = "application/octet-stream";

            Response.AddHeader("Content-Disposition", "attachment; filename=" + Server.UrlEncode(fileName));             Response.BinaryWrite(bytes);             Response.Flush();             Response.End();             return new EmptyResult();

        }

 

View调用:

<href="/Document/DownFile?filePath=@item.Value&fileName=@item.Key">下载</a>  

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。