• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录
nicesoft
博客园    首页    新随笔    联系   管理    订阅  订阅

asp.net点击下载图片 C#

DownloadFile.aspx页面内容:

 

<%@ Page Language="C#" %>

<%

 

//使用方法
DownloadFile.aspx?file=要下载的图片

    string url = HttpContext.Current.Request.Url.Query.ToLower();

    //file=dddd.jpg
    url = url.Replace("?file=", "");

    Response.BufferOutput = false;

    Response.Clear();
    Response.ContentType = "application/x-msdownload";
    Response.AppendHeader("Content-Disposition", "attachment; filename=" + url);
    Response.ContentType = "application/octstream";
    Response.CacheControl = "Private";
    System.IO.Stream stm = new System.IO.FileStream(Server.MapPath(url), System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);
    Response.AppendHeader("Content-length", stm.Length.ToString());

    System.IO.BinaryReader br = new System.IO.BinaryReader(stm);

    byte[] bytes;

    for (Int64 x = 0; x < (br.BaseStream.Length / 4096 + 1); x++)
    {
        bytes = br.ReadBytes(4096);
        Response.BinaryWrite(bytes);
        System.Threading.Thread.Sleep(5);  //休息一下,防止耗用带宽太多。
    }

    stm.Close();

%>

posted @ 2011-12-14 15:55  nicesoft  阅读(1862)  评论(0)    收藏  举报
刷新页面返回顶部
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3