zoukankan      html  css  js  c++  java
  • 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>  

  • 相关阅读:
    Hive内部表外部表转化分析
    20130515
    mapred.local.dir
    经典
    hive中巧用正则表达式的贪婪匹配
    做生意十大忌
    股价是最没有用的东西,要看公司基本面和盈利
    textView文本不同颜色
    Android中扫描wifi热点
    android 与 PC的socket通信
  • 原文地址:https://www.cnblogs.com/fengzhengfly/p/4437127.html
Copyright © 2011-2022 走看看