I wrote my own custom web upload control

Source: Internet
Author: User
Tags border color file upload int size
web| Control | upload | upload control using System;
Using System.Web.UI;
Using System.Web.UI.WebControls;
Using System.ComponentModel;
Using System.Web.UI.HtmlControls;
Using System.IO;
Using System.Drawing;
Using System.Drawing.Design;
Namespace Yingnet.common
{
<summary>
Summary description of the fileupload. E:\program\Common\FileUpload.bmp
</summary>
[ToolboxBitmap (typeof (Yingnet.Common.FileUpload), "Fileupload.bmp"),
Defaultproperty ("Text"), DefaultEvent ("click"),
ToolBoxData ("<{0}:fileupload runat=server></{0}:fileupload>")]
public class FileUpload:System.Web.UI.WebControls.WebControl {
<summary>
Upload button
</summary>
Private button Button=new button ();
<summary>
Number of uploaded files
</summary>
private int filenum=1;
<summary>
File Object
</summary>
Private htmlinputfile[] file;
<summary>
Save path, default to System temp directory
</summary>
private string Path=system.io.path.gettemppath ();
<summary>
Uploaded file name group
</summary>
Private string[] filename;
<summary>
Suffix file name Group
</summary>
Private string[] suffix;
<summary>
Filter, the writing is txt;. Abc
</summary>
private string Filter= "";
<summary>
Limit file upload size, 0 is unrestricted, unit is byte
</summary>
private int Size=0;//system.componentmodel.defaulteventattribute

<summary>
Uploading events
</summary>
[Bindable (True), Category ("event"), Description ("Events fired after upload")
]
public event EventHandler Click;
<summary>
Number of files uploaded
</summary>
[Bindable (True),
Category ("Parameters"), Description ("set the number of uploaded files"),
DefaultValue ("1")]
public int filenum{
set{
if (value<1) {
value=1;
}
Filenum=value;
This. Controls.clear ();
File=new Htmlinputfile[filenum];
Filename=new String[filenum];
Suffix=new String[filenum];
for (int i=0;i<filenum;i++) {
File[i]=new HtmlInputFile ();
This. Controls.Add (File[i]);
}
This. Controls.Add (button);
}
get{
return filenum;
}
}
<summary>
Text of the Upload button
</summary>
[Bindable (True),
Category ("Parameters"), Description ("Set the path of the uploaded file"),
DefaultValue ("1")]
<summary>
Upload path
</summary>
public string Uploadpath {
set{
if ("". Equals (value) | | Value==null) {
Value=system.io.path.gettemppath ();
}
Path=value;
}
get{
return path;
}
}
<summary>
Get file name
</summary>
Public string[] Filename{
get{
return filename;
}
}
<summary>
Get suffix
</summary>
Public string[] Suffix{
get{
return suffix;
}
}
<summary>
Filtering device
</summary>
public string filter{
set{
Filter=value;
}
get{
return filter;
}
}
<summary>
Limit size
</summary>
public int filesize{
set{
Size=value;
}
get{
return size;
}
}
<summary>
Shortcut keys
</summary>
public override string accesskey{
get{
Return button. AccessKey;
}
set{
button. Accesskey=value;
}
}
<summary>
Background
</summary>
public override System.Drawing.Color backcolor{
get{
Return button. BackColor;
}
set{
button. Backcolor=value;
}
}
<summary>
Border color
</summary>
public override System.Drawing.Color bordercolor{
get{
Return button. bordercolor;
}
set{
button. Bordercolor=value;
}
}
<summary>
Border Style
</summary>
public override BorderStyle borderstyle{
get{
Return button. BorderStyle;
}
set{
button. Borderstyle=value;
}
}
<summary>
Text
</summary>
[Bindable (True),
Category ("appearance"),
DefaultValue ("")]
public string Text
{
Get
{
Return button. Text;
}
Set
{
button. Text = value;
}
}

Public FileUpload (): base () {

Filenum=1;
button. Click+=new EventHandler (This.button_click);
This. Controls.Add (button);

}
<summary>
Button Events
</summary>
<param name= "Sender" ></param>
<param name= "E" ></param>
private void Button_Click (object sender, EventArgs e) {
Upload ();
Add your code
if (click!=null)
Click (sender,e); Leaving the event in place
}
<summary>
Upload
</summary>
private void Upload () {
System.Web.HttpPostedFile PostedFile;
for (int i=0;i<filenum;i++) {
try{
Postedfile=file[i]. PostedFile;
if (postedfile!=null) {
if (postedfile.contentlength>size && size!=0) {
Break
}
String Suf=getsuffix (Postedfile.filename);
if (Filter!=null && filter. IndexOf (SUF) >-1) {
Break
}
Filename[i]=datetime.now.ticks.tostring ();
Suffix[i]=suf;
Postedfile.saveas (System.IO.Path.Combine (Path,filename[i]+suf));


}
}finally{
Filename[i]= "";
}
}
}
<summary>
Get suffix Name
</summary>
<param name= "filename" > FileName </param>
<returns> return band. Suffix name </returns>
private string Getsuffix (string filename) {
int Index=filename. LastIndexOf (".");
if (index>0) {
return filename. Substring (index);
}
Return "";
}

}
}


Related Article

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.