如何通过浏览器将远程图片下载

新建一个页面DownloadFile.aspx

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Drawing.Imaging" %>
<%@ Import Namespace="System.IO" %>
<%@ Import Namespace="System.Net" %>
<%@ Import Namespace="k8008.Utils" %>

<%  
string url = Fetch.GetString("url");

WebRequest wreq = WebRequest.Create(url);
HttpWebResponse wresp = (HttpWebResponse)wreq.GetResponse();
Stream s = wresp.GetResponseStream();
System.Drawing.Image img;
img = System.Drawing.Image.FromStream(s);
MemoryStream ms = new MemoryStream();
img.Save(ms, ImageFormat.Gif);
img.Dispose();
Response.AddHeader("Content-Disposition", "attachment; filename=" + HttpUtility.UrlEncode("qrcode.jpg", System.Text.Encoding.UTF8));
Response.BinaryWrite(ms.ToArray());
Response.Flush();
Response.End();
%>

 

$("#download").click(function () {
            var src = escape($("#code").attr("src"));

            window.location.href = "DownloadFile.aspx?url=" + src;
        });

 

posted on 2016-07-21 15:39  阳光秋天  阅读(314)  评论(0编辑  收藏  举报