GetPhoto.aspx
<%@ Page language="c#" Codebehind="GetPhoto.aspx.cs" AutoEventWireup="false" Inherits="tt.GetPhoto" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>GetPhoto</title>
        <meta content="Microsoft Visual Studio .NET 7.1" name="GENERATOR">
        <meta content="C#" name="CODE_LANGUAGE">
        <meta content="JavaScript" name="vs_defaultClientScript">
        <meta content="http://schemas.microsoft.com/intellisense/ie5" name="vs_targetSchema">
    </HEAD>
    <body>
        <form id="Form1" method="post" runat="server">
            <P>QQ号码:<asp:textbox id="txtQQ" runat="server"></asp:textbox>
                <asp:button id="Button1" runat="server" Text="读取相册"></asp:button></P>
            <asp:Label ID="msg" Runat="server"></asp:Label>
            <P>
                <asp:DataList id="albumList" Runat="server" RepeatColumns="8" EnableViewState="False">
                    <ItemTemplate>
                        <div>
                            <div><%# DataBinder.Eval(Container.DataItem,"PicLink")%></div>
                            <div><%# DataBinder.Eval(Container.DataItem,"PicTitle")%></div>
                        </div>
                    </ItemTemplate>
                </asp:DataList>
                <asp:DataList id="picList" Runat="server" RepeatColumns="8" EnableViewState="False" DataKeyField="PicUrl">
                    <ItemTemplate>
                        <div>
                            <div><%# DataBinder.Eval(Container.DataItem,"PicLink")%></div>
                            <div>
                                <asp:CheckBox ID="confirm" Runat="server" Text='<%# DataBinder.Eval(Container.DataItem,"PicTitle")%>' /></div>
                        </div>
                    </ItemTemplate>
                    <FooterTemplate>
                        <div>
                            <asp:Button ID="bntOk" Runat="server" CommandName="Update" Text="开始导入"></asp:Button></div>
                    </FooterTemplate>
                </asp:DataList>
            </P>
        </form>
    </body>
</HTML>

解决方案 »

  1.   

    续上,cs页代码:
    GetPhoto.aspx.csusing System;
    using System.Collections;
    using System.Data;
    using System.Web;
    using System.Web.SessionState;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.Net;
    using System.IO;
    using System.Xml;
    using System.Drawing.Imaging;namespace tt
    {
        /**//// <summary>
        /// GetPhoto 的摘要说明。
        /// </summary>
        public class GetPhoto : System.Web.UI.Page
        {
            protected System.Web.UI.WebControls.TextBox txtQQ;
            protected System.Web.UI.WebControls.Button Button1;
            protected System.Web.UI.WebControls.DataList picList; //{0}qq号
            protected System.Web.UI.WebControls.Label msg;  
            protected System.Web.UI.WebControls.DataList albumList;
        
            private const string QQPHOTO = "http://p{0}.photo.qq.com/{1}/16"; //{0} = {1} % 13 + 1 {1}为qq号
            //取相册的另一个有用路径,在第一个可用的情况下用。
            private const string QQPHOTO_B = "http://photo.qq.com/cgi-bin/common/cgi_list_album?uin={0}";
            private const string ALBUMURL = "http://sz.photo.store.qq.com/http_staload.cgi?{0}/{1}"; //{0}qq号 {1}album号
            
            private string UserID = "maxwell"; //用户名        private void Page_Load(object sender, System.EventArgs e)
            {
                //点击“开始导入”时 把打勾的图片保存到本机上。
                picList.UpdateCommand += new DataListCommandEventHandler(picList_UpdateCommand);            //取url参数中的相册图片 即取qq相册   QQ有防盗链
                if(Request.QueryString["pre"] != null) 
                {
                    string url = Request.QueryString["pre"];                WebRequest preR = WebRequest.Create(url);
                    Stream stream = preR.GetResponse().GetResponseStream();
                    //QQ相册只能上传jpg|gif|png的图片
                    //把这些图片保存为jpeg格式和ContentType设置为image/jpeg都能显示。
                    Response.ClearContent();
                    Response.ContentType = "image/jpeg";                System.Drawing.Bitmap img = new System.Drawing.Bitmap(stream);        
                    img.Save(Response.OutputStream,ImageFormat.Jpeg);                img.Dispose();
                    stream.Close();
                    Response.End();
                }            //根据相册ID取该相册中的图片。
                if(Request.QueryString["id"] != null && Request.QueryString["qq"] != null)
                {
                    string id = Request.QueryString["id"];
                    string qq = Request.QueryString["qq"];
                    string albumUrl = string.Format(ALBUMURL,qq,id);
                    
                    XmlDocument xml = GetXmlData(albumUrl);
                    if(xml == null)
                    {
                        msg.Text = "暂时不能读取数据。请稍后再试。";
                        return;
                    }
                    XmlNodeList list = xml.GetElementsByTagName("pic");                PicAlbum[] pics = new PicAlbum[list.Count];                for(int i=0; i<list.Count; i++)
                    {
                        string name = list[i].SelectSingleNode("name").InnerText;
                        string pre = list[i].SelectSingleNode("pre").InnerText;
                        string url = list[i].SelectSingleNode("url").InnerText;
                    
                        string picLink = "<img src='" + Request.Url.AbsolutePath + "?pre=" + pre + "'/>";
                        pics[i] = new PicAlbum(url,picLink,name);
                    }
                    albumList.Visible = false;
                    picList.Visible = true;
                    picList.DataSource = pics;
                    picList.DataBind();
                }
        }     
      

  2.   

    续上,cs页代码
      
     Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
            override protected void OnInit(EventArgs e)
            {
                //
                // CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
                //
                InitializeComponent();
                base.OnInit(e);
            }
            
            /**//// <summary>
            /// 设计器支持所需的方法 - 不要使用代码编辑器修改
            /// 此方法的内容。
            /// </summary>
            private void InitializeComponent()
           {    
                this.Button1.Click += new System.EventHandler(this.Button1_Click);
                this.Load += new System.EventHandler(this.Page_Load);        }
            #endregion        //读取相册
            private void Button1_Click(object sender, System.EventArgs e)
            {
                string qq = txtQQ.Text;
                string photoUrl = string.Format(QQPHOTO,(int.Parse(qq)%13+1),qq);
                
                //从QQ读取相册数据
                XmlDocument xml = GetXmlData(photoUrl);
                if(xml == null)
                {
                    //判断静态XML是否取到,如果未取到,再尝试拉一次CGI(QQ的说法)
                    string photoBackupUrl = string.Format(QQPHOTO_B,qq);
                    xml = GetXmlData(photoBackupUrl);
                    if(xml == null)
                  {
                        msg.Text = "暂时不能读取数据。请稍后再试。";
                        return;
                    }
                }
                XmlNodeList list = xml.GetElementsByTagName("album");
                
                ArrayList albums = new ArrayList();
                for(int i=1; i<list.Count; i++)//从1开始,是因为xml文件的第一个album不是相册数据。
                {
                    string priv = list[i].SelectSingleNode("priv").InnerText.Trim();
                    if(priv != "1")
                        continue;
                    string id = list[i].SelectSingleNode("id").InnerText;
                    string name = list[i].SelectSingleNode("name").InnerText;
                    string pre = list[i].SelectSingleNode("pre").InnerText;
                    
                    string picLink = "<a href='"+ Request.Url.AbsolutePath + "?id=" + id +"&qq=" + qq + "'><img src='" + Request.Url.AbsolutePath + "?pre=" + pre + "'/></a>";
                    albums.Add(new PicAlbum(id,picLink,name));
                    
                }
                picList.Visible = false;
                albumList.Visible = true;
                albumList.DataSource = albums;
                albumList.DataBind();
            }
            /**//// <summary>
            /// 根据地址得到一个xml文档
            /// </summary>
            /// <param name="url">地址</param>
            /// <returns></returns>
            private XmlDocument GetXmlData(string url)
            {
                HttpWebRequest wreq = (HttpWebRequest)WebRequest.Create(url);
                //wreq.Timeout = 20;
                HttpWebResponse wres = null;
                XmlDocument xml = null;
                try
                {
                    wres = (HttpWebResponse)wreq.GetResponse();
                    xml = new XmlDocument();
                    xml.Load(wres.GetResponseStream());
                }
                finally
               {
                    wres.Close();
                }
                return xml;
            }        /**//// <summary>
            /// 开始导入图片  只把打勾的图片导入。
            /// </summary>
            /// <param name="source"></param>
            /// <param name="e"></param>
            private void picList_UpdateCommand(object source, DataListCommandEventArgs e)
            {
                DataList picList = source as DataList;
                PicAlbum[] pics = (PicAlbum[])picList.DataSource;
                ArrayList urls = new ArrayList(); //用来保存要导入图片的地址。
                for(int i=0; i<picList.Items.Count; i++)
                {
                    CheckBox cb = (CheckBox)picList.Items[i].FindControl("confirm");
                    if(cb.Checked)
                    {
                        urls.Add(picList.DataKeys[i]);
                    }
                }
                //读取要保存图片的URL后,保存图片
                SavePics(urls);
            }        /**//// <summary>
            /// 保存列表中的图片。
            /// </summary>
            /// <param name="urls">保存图片地址的列表</param>
            private void SavePics(ArrayList urls)
            {
                DateTime date = DateTime.Now;
                string path = Server.MapPath(".");
                int i=1;
                foreach(string url in urls)
               {
                    string name = path + "\" + UserID + date.ToString("yyyyMMdd") + date.Hour.ToString() 
                        + date.Minute.ToString() + date.Second.ToString() 
                        + (i++)
                        + ".jpg";
                    HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url);
                    Stream stream = req.GetResponse().GetResponseStream();                System.Drawing.Bitmap img = new System.Drawing.Bitmap(stream);        
                    img.Save(name,ImageFormat.Jpeg);                img.Dispose();
                    stream.Close();
                }
                msg.Text = "文件保存完成。";
            }
        }    /**//// <summary>
        /// 图片相册类
        /// </summary>
        public class PicAlbum
        {
            private string picUrl;
            private string picLink;
            private string picTitle;        public PicAlbum(string picurl,string piclink,string pictitle)
            {
                picUrl = picurl;
                picLink = piclink;
                picTitle = pictitle;
            }        public string PicUrl
            {
                get{ return picUrl;}
            }
            public string PicLink
            {
                get{ return picLink;}
            }
            public string PicTitle
            {
                get{ return picTitle;}
            }
        }
    }
      

  3.   

    我晕汗刚才只看见aspx代码代码这么多
      

  4.   

    靠 vs08真的没问题吗?  有那位大侠可以改到VS05的吗?
      

  5.   

    1 .  public class GetPhoto : System.Web.UI.Page 
         改为:
          public partial class GetPhoto : System.Web.UI.Page 
    2 .  string name=path+"\"+userid
         改为:
         string name=path+"\\"+userid
      

  6.   

    还有两个.
    3.   
    <%@ Page language="c#" Codebehind="GetPhoto.aspx.cs" AutoEventWireup="false" Inherits="tt.GetPhoto" %>
    改为:
    <%@ Page language="c#" CodeFile="GetPhoto.aspx.cs" AutoEventWireup="false" Inherits="GetPhoto" %>4.  cs代码中不要定义命名空间.我怎么感觉你这代码像是vs2003的啊?
      

  7.   

    因为你的这个是vs 2003做的,所以在vs 2005环境下要修改的内容为:
    1.新建一个 .aspx文件,并只复制
    <form >
    ...........
    </form>
    的内容到新建的页面
    2.
     <asp:button id="Button1" runat="server" Text="读取相册"></asp:button>
    改成:
    <asp:button id="Button1" runat="server" Text="读取相册" OnClick="Button1_Click"></asp:button>
    3.给 id="picList" 的datalist控件添加上  picList_UpdateCommand事件4..aspx.cs文件中只复制 所有的方法和定义的常量和变量,下面的代码不要复制:
    protected System.Web.UI.WebControls.TextBox txtQQ; 
            protected System.Web.UI.WebControls.Button Button1; 
            protected System.Web.UI.WebControls.DataList picList; //{0}qq号 
            protected System.Web.UI.WebControls.Label msg;  
            protected System.Web.UI.WebControls.DataList albumList; 
      

  8.   

    转换2003的工程到2005, IDE会帮你转换。
      

  9.   


    何解呢?怎样添加这个事件!我是学ASP的,不懂,可以写出来怎么加入这个事件吗?