A simple file Upload function control

Source: Internet
Author: User
Tags file size file upload tostring
Control | Upload always use someone else's control, the first time you want to write a control. Then wrote a simple small control, mainly for their own learning and other want to try to write the control of friends, please advise.
The main role of the control is to upload the local picture to the custom directory.
Using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.Web.UI.HtmlControls;
Using System.IO;

Namespace Uploadimage
{
<summary>
Author: Darkangel 2004-10-27 Day of Creation
Support image upload to server function
</summary>
[Defaultproperty ("Text"),
ToolBoxData (@ "<{0}:upimage runat=server></{0}:upimage>")]
public class Upimage:control, INamingContainer
{
protected int filelength;
protected string ImageUrl;
protected string mydirectory;
static string Logourl;
protected string vpicture;
Public Button MyButton;
Public HtmlInputFile FileUpload;
Public Label Label1;
Public Upimage ()
{
This. EnsureChildControls ();

}
[Bindable (True),
Category ("appearance"),
DefaultValue ("")]
[
DescriptionAttribute ("File Size")
]
public int Filelength
{
Set{filelength=value;}
Get{return Filelength;}
}

[
DescriptionAttribute ("Picture name")
]
public string ImageUrl
{
Set{imageurl=value;}
Get{return ImageUrl;}
}

[
DescriptionAttribute ("File path")
]
public string Mydirectory
{
Get{return mydirectory;}
Set{mydirectory=value;}
}

[
DescriptionAttribute ("Relative address of picture")
]
public string Logo
{

Get{return Logourl;}
Set{logourl=value;}
}

[
DescriptionAttribute ("Show Pictures")
]
public string Vpicture
{
Set{vpicture=value;}
Get{return Vpicture;}
}

private void MyButton_Click (object sender, System.EventArgs e)
{

if (!fileupload.value.tostring (). Equals (""))
{
Logourl=fileupload.postedfile.filename.tostring ();

Logourl=logourl.substring (Logourl.lastindexof ("."), (Logourl.length-logourl.lastindexof ("."));
if (fileupload.postedfile.contentlength>filelength)
{
MyScript ("The picture exceeds the specified size!") ");

}
Else
{
if (Logourl.equals (". jpg") | | Logourl.equals (". bmp") | | Logourl.equals (". gif"))
{

logourl=mydirectory+ "\" +imageurl+logourl;
Mydirectory=page.server.mappath ("") + "\" +mydirectory;


if (directory.exists (mydirectory))
{
}
Else
{
Directory.CreateDirectory (mydirectory);
}
FileUpload.PostedFile.SaveAs (Page.Server.MapPath ("") + "\" +logourl);




if (vpicture. Equals ("1"))
{
label1.text= "}

MyScript ("Picture upload success!") ");

}
Else
{
MyScript ("Wrong file type!") ");

}

}
}

}
protected void MyScript (string java)
{
Page.registerstartupscript ("FSF", "<script Language=javascript>alert" (' +java+ ");</script>");

}

protected override void CreateChildControls ()
{
Mybutton=new Button ();
Fileupload=new HtmlInputFile ();
Label1=new Label ();
MyButton. Text= "Submit";

This. Controls.Add (FileUpload);
This. Controls.Add (MyButton);
This. Controls.Add (New LiteralControl ("<p>"));
This. Controls.Add (LABEL1);
This. Controls.Add (New LiteralControl ("</p>"));
MyButton. Click+=new EventHandler (MyButton_Click);

}
}
}



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.