Store the image to XML and display the image

Source: Internet
Author: User

Store Image to XML:
Byte [] filebytearray = new byte [filelength];
// Temporarily store 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 );

Show image:

int imgid = convert. toint32 (request. querystring ["ID"]); // ID is the image id
// create a database link
string filename = server. mappath (". \ writexml. XML "); // file to be opened
xmldocument xmldoc = new xmldocument ();
xmldoc. load (filename);
xmlnodelist node = xmldoc. selectsinglenode ("// image [imageid = '" + imgid. tostring () + "']"). childnodes;
If (node! = NULL)
{< br> 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 ();

// you can save it 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.