zoukankan      html  css  js  c++  java
  • MVC 之下载 我的实践

    Controller

    1、

    public ActionResult DownLoad(string path,string fileName)

    {

    return File(new FileStream(path, FileMode.Open), "application/octet-stream", Server.UrlEncode(fileName));

    }

    2、

    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>  

  • 相关阅读:
    回家第二周
    回文
    回家第一周
    回家第四周
    《大道至简》读后感
    第三周Java课后题
    Java小测代码及截图
    【CheckBox】选择或取消所有CheckBox
    Web Developer's Handbook
    Oracle PL/SQL
  • 原文地址:https://www.cnblogs.com/DataBase-123/p/6081136.html
Copyright © 2011-2022 走看看