左键点击下载图片

1、后台代码,用ashx处理,输出二进制流图片

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;

namespace FootBallCalendar
{

    public class outimg : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            string strFilePath = context.Server.MapPath("toimg/to1000eurohigh.png");

            FileStream fs = new FileStream(strFilePath, FileMode.Open);
            byte[] bytes = new byte[(int)fs.Length];
            fs.Read(bytes, 0, bytes.Length);
            fs.Close();
            context.Response.Clear();
            context.Response.ContentType = "application/octet-stream";
            context.Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("eur.png", System.Text.Encoding.UTF8));
            context.Response.BinaryWrite(bytes);
            context.Response.Flush();
            context.Response.End();        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}

 


2、前台代码

<script type="text/javascript">
function runSave(){
if (saveImg.location != "about:blank")window.saveImg.document.execCommand("SaveAs");
}

function saveImgAs(url){
if(window.saveImg && url)window.saveImg.location=url;
}

</script>

<a href="#" onclick="saveImgAs('http://xxxxx/outimg.ashx')"><img src="/xxxx/img/input01.jpg" border="0"/></a>

 

posted on 2012-08-05 14:48  王阿冰  阅读(177)  评论(0编辑  收藏  举报

导航