protected void btnSumit_Click(object sender, EventArgs e)
{
System.Drawing.Image img;
conn.Open();
using (SqlCommand cmd = new SqlCommand())
{
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
string path = Server.MapPath("XML.xml");
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNodeList node = xml.GetElementsByTagName("resault");
foreach (XmlElement n in node)
{
string strData = n.ChildNodes[1].InnerText;
int nSize = n.ChildNodes[1].InnerText.Length;
// Response.OutputStream.Write(Convert.FromBase64String(strData), 0, nSize);
MemoryStream ms = new MemoryStream(Convert.FromBase64String(strData));
img = System.Drawing.Image.FromStream(ms);
string url = Server.MapPath("img/");
DirectoryInfo dir = new DirectoryInfo(url);
FileInfo[] filecount = dir.GetFiles();
int i = filecount.Length;
string imgName=url+((i+1)+".jpg");
img.Save(imgName);
cmd.CommandText = string.Format("insert into img2(title,img) values('" + n.ChildNodes[0].InnerText+ "','"+imgName+"')");
cmd.ExecuteNonQuery();
ClientScript.RegisterStartupScript(this.GetType(), "", "<script>alert('导入成功')</script>");
}
}
conn.Close();
}
XML结构:
<?xml version="1.0" standalone="yes"?>
<DocumentElement>
<resault>
<id>179</id>
<img>"图片流"</img>
</resault>
<resault>
<id>180</id>
<img>"图片流"</img>
</resault>
<DocumentElement>