Save the image to an XML file and retrieve the image display from the XML file.

Source: Internet
Author: User
1. Save it to the XML file // get the file name to be uploaded by the user
String strFilePathName = loFile. PostedFile. FileName;
String strFileName = Path. GetFileName (strFilePathName );
Int FileLength = loFile. PostedFile. ContentLength;
If (FileLength <= 0)
Return;
Try
{
Byte [] FileByteArray = new Byte [FileLength]; // temporary storage of Byte Arrays for image files
Stream StreamObject = loFile. PostedFile. InputStream; // create a data Stream object
// Read image file data. FileByteArray is the data storage body, 0 is the Data Pointer position, and filelne is the data length.
StreamObject. Read (FileByteArray, 0, FileLength );
String fileName = Server. MapPath (". \ WriteXml. xml"); // the file to be opened

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (fileName );
XmlNode root = xmlDoc. SelectSingleNode ("dbImage"); // search for <dbGuest>
XmlNodeList xnl = xmlDoc. SelectSingleNode ("dbImage"). ChildNodes;
Int nIndex = xnl. Count;
// Add the following Node
XmlElement xe1 = xmlDoc. CreateElement ("Image"); // create a <User> node
XmlElement xesub1 = xmlDoc. CreateElement ("ImageID ");
Xesub1.InnerText = nIndex. ToString (); // set the text node
Xe1.AppendChild (xesub1); // Add it to the <User> node
XmlElement xesub2 = xmlDoc. CreateElement ("ImageContentType ");
Xesub2.InnerText = loFile. PostedFile. ContentType;
Xe1.AppendChild (xesub2 );
XmlElement xesub3 = xmlDoc. CreateElement ("ImageSize ");
Xesub3.InnerText = FileLength. ToString ();
Xe1.AppendChild (xesub3 );
XmlElement xesub4 = xmlDoc. CreateElement ("ImageDescription ");
Xesub4.InnerText = tbDescription. Text;
Xe1.AppendChild (xesub4 );
XmlElement xesub5 = xmlDoc. CreateElement ("ImageData ");
Xesub5.InnerText = Convert. ToBase64String (FileByteArray );
Xe1.AppendChild (xesub5 );


Root. AppendChild (xe1); // Add it to the <dbGuest> node.
XmlDoc. Save (fileName );

Response. Redirect ("ShowAllImg. aspx ");
}
Catch
{
}

2. Retrieve and display int ImgID = Convert. ToInt32 (Request. QueryString ["ID"]) from the XML file; // ID is the image ID
// Create a database link
String fileName = Server. MapPath (". \ WriteXml. xml"); // the file to be opened

XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (fileName );
XmlNodeList node = xmlDoc. SelectSingleNode ("// Image [ImageID = '" + ImgID. ToString () + "']"). ChildNodes;
If (node! = Null)
{
String strType = node. Item (1). InnerText;
String strData = node. Item (4). InnerText;
Int nSize = int. Parse (node. Item (2). InnerText );

Response. ContentType = strType; // set the output file type
// Binary number of output image files
Response. OutputStream. Write (Convert. FromBase64String (strData), 0, nSize );
Response. End ();
// It can also be saved as an image
// FileStream fs = new FileStream (@ "C: \ aa. BMP", FileMode. OpenOrCreate, FileAccess. Write );
// Fs. Write (Convert. FromBase64String (strData), 0, nSize );
// Fs. Close ();
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.