62,194
社区成员
发帖
与我相关
我的任务
分享
<%@ 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>
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;}
}
}
}