1. Save the image to an XML file
Copy codeThe Code is as follows:
/// <Summary>
/// Save the image to an XML file
/// </Summary>
Private void UploadImageToXml ()
{
/// Get the file name to be uploaded
String strFilePathName = loFile. PostedFile. FileName;
String strFileName = Path. GetFileName (strFilePathName );
Int FileLength = loFile. PostedFile. ContentLength;
If (FileLength <= 0)
Return;
Try
{
/// Temporarily store Byte Arrays for image files
Byte [] FileByteArray = new Byte [FileLength];
/// Create a data stream object
Stream StreamObject = loFile. PostedFile. InputStream;
/// 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 );
/// File to be opened
String fileName = Server. MapPath (". \ WriteXml. xml ");
XmlDocument xmlDoc = new XmlDocument ();
XmlDoc. Load (fileName );
/// Search for <dbGuest>
XmlNode root = xmlDoc. SelectSingleNode ("dbImage ");
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 ");
/// Set the text node
Xesub1.InnerText = nIndex. ToString ();
/// Add to the <User> node
Xe1.AppendChild (xesub1 );
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 );
/// Add to the <dbGuest> node
Root. AppendChild (xe1 );
XmlDoc. Save (fileName );
Response. Redirect ("ShowAllImg. aspx ");
}
Catch (Exception ex)
{
Throw ex;
}
}
Ii. Reading image data from XML
Copy codeThe Code is as follows:
/// <Summary>
/// Read the image from XML
/// </Summary>
/// <Param name = "ImageID"> image ID </param>
Private void ReadImageFromXml (string ImageID)
{
/// ID is the image ID
Int ImgID = Convert. ToInt32 (ImageID );
/// File to be opened
String fileName = Server. MapPath (". \ WriteXml. xml ");
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 );
/// Set the output file type
Response. ContentType = strType;
/// 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 ();
}
}