zoukankan      html  css  js  c++  java
  • MVC下载文件方式

    MVC下载文件方式

    http://www.cnblogs.com/liang--liang/archive/2012/10/20/2732745.html

    方式一:

    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

  • 相关阅读:
    lower_bound和upper_bound
    memcache教程
    php header() 函数
    修复TortoiseGit文件夹和文件图标不显示
    mysql数据类型
    PDO类PDO、PDOStatement、PDOException
    mysqli和pdo对mysql数据库的操作
    php 关键字
    几个入口文件定义函数
    php --魔术常量 /魔术方法
  • 原文地址:https://www.cnblogs.com/thaughtZhao/p/4996884.html
Copyright © 2011-2022 走看看