码迷,mamicode.com
首页 > 其他好文 > 详细

通过浏览器下载服务端图片到客户端

时间:2016-07-22 08:50:39      阅读:153      评论:0      收藏:0      [点我收藏+]

标签:

<%@ Page Language="C#" %>  
  
<%  
    //使用方法  
    //DownloadFile.aspx?file=要下载的图片url  
  
    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();  
  
%> 

前端调用下载

$("#download").click(function() {
            window.location.href = "DownloadFile.aspx?file=" + "/kjzbsc/qrcode.jpg";
        });

 

通过浏览器下载服务端图片到客户端

标签:

原文地址:http://www.cnblogs.com/sophiel/p/5693802.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!