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 ();
}