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.