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>  

  • 相关阅读:
    2014年互联网发展趋势如何
    服务器出现阶段性错误
    用互联网思想武装自己
    杭州互联网公司汇总
    互联网牛人网址大全
    ffmpeg开发指南
    Windows下FFmpeg快速入门
    FFmpeg介绍及参数详细说明
    windows 下FFMPEG的编译方法 附2012-9-19发布的FFMPEG编译好的SDK下载
    FFMPEG视音频编解码零基础学习方法 【荐】
  • 原文地址:https://www.cnblogs.com/fengzhengfly/p/4437127.html
Copyright © 2011-2022 走看看