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>  

  • 相关阅读:
    python学习第18天----属性、类方法、静态方法
    面试总结
    JAVA面试题整理
    Docker-基础
    Shell
    MYSQL
    logstash的使用(ELK)
    (ELK)FileBeat的使用
    Zookeeper的介绍和单机、集群搭建
    Elaticsearch7.7.0的安装(ELK)
  • 原文地址:https://www.cnblogs.com/DataBase-123/p/6081136.html
Copyright © 2011-2022 走看看