zoukankan      html  css  js  c++  java
  • 文件下载简单示例

    public void FileDownload2()
    {
    string fileName = "新建文件夹2.rar";//客户端保存的文件名
    string filePath = Server.MapPath("/App_Data/新建文件夹2.rar");//要被下载的文件路径

    Response.ContentType = "application/octet-stream";//二进制流
    //通知浏览器下载文件而不是打开
    Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode(fileName, System.Text.Encoding.UTF8));

    //以字符流的形式下载文件
    using (FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read))
    {
    Response.AddHeader("Content-Length", fs.Length.ToString());
    //这里容易内存溢出
    //理论上数组最大长度 int.MaxValue 2147483647
    //(实际分不到这么多,不同的程序能分到值也不同,本人机器,winfrom( 2147483591 相差56)、iis(也差不多2G)、iis Express(只有100多MB))
    byte[] bytes = new byte[(int)fs.Length];
    fs.Read(bytes, 0, bytes.Length);
    Response.BinaryWrite(bytes);
    }
    Response.Flush();
    Response.End();
    }

  • 相关阅读:
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    Live2d Test Env
    偷东西的学问-背包问题
    HMM-前向后向算法理解与实现(python)
    详解数组分段和最大值最小问题(最小m段和问题)
    打家劫舍系列
    面试题56
  • 原文地址:https://www.cnblogs.com/wslpf/p/9502437.html
Copyright © 2011-2022 走看看