Save picture as XML and retrieve pic from XML file

Source: Internet
Author: User
<Form ID = "form1" runat = "server">
<Asp: fileupload id = "fileupload1" runat = "server"/>
<Asp: button id = "button1" runat = "server" text = "button" onclick = "button#click"/>
<Asp: button id = "button2" runat = "server" onclick = "button2_click" text = "button"/>
</Form>

// Code Using system;
Using system. collections;
Using system. configuration;
Using system. Data;
Using system. LINQ;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. htmlcontrols;
Using system. Web. UI. webcontrols;
Using system. IO;
Using system. xml;
Using system. xml. serialization;
Using system. text;
Public partial class pages_pagetest: Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void button#click (Object sender, eventargs E)
{
Stream f_stream = This. fileupload1.postedfile. inputstream;
Binaryreader BR = new binaryreader (f_stream, system. Text. encoding. Default );
Byte [] DATA = new byte [f_stream.length];
BR. Read (data, 0, Data. Length );
Stringbuilder strb = new stringbuilder ();
Strb. append (convert. tobase64string (data ));
Pic pbj = new PIC ();
PBJ. s_pid = strb. tostring ();
PBJ. ID = 3;
Filestream FS = NULL;
Try
{
Xmlserializer xs = new xmlserializer (typeof (PIC ));
FS = new filestream (server. mappath ("~ /A. xml "), filemode. Create, fileaccess. Write );
Xs. serialize (FS, PBJ );

FS. Close ();
}
Catch (exception ex)
{
If (FS! = NULL)
FS. Close ();
Throw new exception ("XML serialization failed! ");
}



}
Protected void button2_click (Object sender, eventargs E)
{

Filestream FS = NULL;
Try
{

Xmlserializer xs = new xmlserializer (typeof (PIC ));
FS = new filestream (server. mappath ("~ /A. xml "), filemode. Open, fileaccess. readwrite );
Pic obj = Xs. deserialize (FS) as PIC;


Byte [] DATA = convert. frombase64string (obj. s_pid );
Response. binarywrite (data );
Response. End ();
FS. Close ();
}
Catch (exception ex)
{
If (FS! = NULL)
FS. Close ();
Throw new exception ("XML serialization failed! ");
}
}
}
Public class PIC
{
Public String s_pid;
Public int ID;
}

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.