Compile the upload Program

Source: Internet
Author: User
Drag the fileupload control and the button < Div >
< ASP: fileupload ID = "M_file" Runat = "Server"   /> & Nbsp;
< ASP: button ID = "Button1" Runat = "Server" Onclick = "Button#click" Text = "Button"   />
< BR />
< ASP: Label ID = "Label1" Runat = "Server" Text = "Label" > </ ASP: Label >
</ Div >

Button click event

Itsolutionconfig ISC = (itsolutionconfig) configurationmanager. getsection ("itsolutionconfiguration ");
Protected void page_load (Object sender, eventargs E)
{

}
Protected   Void Button#click ( Object Sender, eventargs E)
{
Arraylist al = Shared. splitsomebody (ISC. type );
Int Size = ISC. size;
If (M_file.postedfile.contentlength > Size |   ! Al. Contains (m_file.postedfile.contenttype ))
{
Response. Write ( " <SCRIPT Lauguage = 'javascript '> alert ('the image you uploaded is too large or the type does not match! '); History. Back (); </SCRIPT> " );

}
Else
{
M_file.saveas (server. mappath ( " . " ) +   " \ Upload \\ "   + M_file.filename );
Label1.text =   " Uploaded successfully! " ;
Label1.forecolor = System. Drawing. color. Red;
}
}

in the web. set the itsolutionconfiguration node configsections >
section name =" itsolutionconfiguration " type =" itsolution. config. itsolutionconfig " />
configsections >

<ItsolutionconfigurationSize= "1024000"Type= "Image/GIF | image/pjpeg"/>

The shared. splitsomebody (ISC. Type) method is as follows: ///   <Summary>
/// Break down the somebody character into an arraylist.
/// For example: image/GIF | image/pjpeg, separated by "|" and placed in the arraylist.
///   </Summary>
Public   Static Arraylist splitsomebody ( String Somebody)
{
Arraylist al =   New Arraylist ();
String [] Alist = Somebody. Split ( ' | ' );
Foreach ( String Arlist In Alist)
{
Al. Add (arlist );
}
Return Al;
}

In this case, we can set the size and type of the uploaded file in Web. config.

How to set the itsolutionconfiguration node?
Add an itsolutionconfig class Namespace Itsolution. config
{
///   <Summary>
/// Set the itsolutionconfiguration node attribute value in the web. config file
///   </Summary>
Public   Class Itsolutionconfig: configurationsection
{
Public Itsolutionconfig ()
{
//
// Todo: Add constructor logic here
//
}
[Configurationproperty ( " Size " )]
Public   Int Size
{
Get { Return ( Int ) This [ " Size " ];}
Set { This [ " Size " ] = Value ;}
}
[Configurationproperty ( " Type " )]
Public   String Type
{
Get { Return ( String ) This [ " Type " ];}
Set { This [ " Type " ] = Value ;}
}

}
}

Then we can register it in the web. config file.
1: Add attributes to the itsolutionconfig class.
2: Set the attribute value in Web. config.

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.