利用 iframe 来实现打印和图片下载

图片下载原理:


(1)用 <a> 指向一个图片,然后在新窗口中打开,默认情况,只是在新窗口中打开图片,实现不了下载,要实现下载,必须用 aspx 页面,重写。

      代码如下:


private void GetMapByWMS()

    {

        try

        {

            Response.Clear();

            Response.ContentType = "application/octet-stream";

            Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("9999.jpg", System.Text.Encoding.UTF8));


            string strUrl = "";

            if (Request.Params["img"] != null)

            {

                strUrl = Request.Params["img"].ToString();

            }

            else

            {

                Response.Write("参数出错,请不要直接访问本文站!");

                Response.End();

                return;

            }


            HttpWebRequest HttpWReq = (HttpWebRequest)WebRequest.Create(strUrl);

            HttpWReq.Method = "Get";

            HttpWReq.ContentLength = 0;

            HttpWebResponse HttpWResp = (HttpWebResponse)HttpWReq.GetResponse();

            Stream strm = HttpWResp.GetResponseStream();

            int count1 = (int)HttpWResp.ContentLength;


            if (count1 == -1)

            {

                count1 = 1024 * 1024 * 5;

            }



            byte[] buffer = new byte[count1];

            int c = 0;

            int k = strm.Read(buffer, 0, count1);

            c = c + k;

            // 读几次,直到读完

            while (k != 0)

            {

                k = strm.Read(buffer, c, count1 - c);

                c = c + k;

            }


            strm.Close();

            Response.OutputStream.Write(buffer, 0, buffer.Length);

        }

        catch (Exception ee)

        {

            Response.Write(ee.Message);

        }

    }

(2) 用 <a> 指向这 页面,就会下载。

(3)如何用按钮触发下载。用 iframe, 用 js 代码将 ifarme 指向上面那个页面,就会实现单击按钮下载图片。 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值