Web control development-perfect upload and download control "new" (I)

Source: Internet
Author: User

In order to increase popularity, I regret this new author :). I will parse the upload and download controls I have developed one by one, hoping to slightly increase popularity :). Also, please visit the sample site of this control.

I. control development ideas

At present, many projects developed by myself will encounter file upload and download problems. To solve all these headaches at one time, I specially developed this component. To meet most application scenarios, I specifically summarized the three scenarios where the upload and download controls are needed before development, and made the self-developed controls fully fit into these three scenarios.

Integration:

The file name uploaded to the server is the name specified by the server. For example, I want to maintain the driver for an electronic product.ProgramThe project requires that the downloaded file name be

String. Format ("%0%_%1%.zip", productid, osname ). For example, ap2000_winxp.zip.

Scenario 2:

The file name uploaded to the server is automatically generated. It is mainly used for scenarios where the number of uploaded files is large and there are no requirements for the name, such as the pictures in the news.

Scenario 3:

The file uploaded to the server is the client file name, just like inserting images or downloading files in a blog.

In order to satisfy the above three situations, I specifically declared the following enumeration classes:

Public   Enum Filenametype

{

Autogenerate,

Clientside,

Serverside

}

Declare the following attributes at the same time:

[

Notifyparentproperty ( True ),

Category ( " Appearance " ),

Defaultvalue ( Typeof (Filenametype ), " Clientside " )

]

Public Filenametype uploadfilenametype

{

Get

{< br> Object O = viewstate [uploadfilenametypekey];
return (O =< /Span> null ) ? filenametype. clientside :( filenametype) O;

}

Set

{

Viewstate [uploadfilenametypekey]=Value;

}

}


Generally, file operations are nothing more than upload, download, and delete operations. Therefore, there is no special interface element on the interface and there is no complicated drawing requirement, therefore, I decided to use a composite control for its development, and we did not care about its height, background color, Font, and so on for the upload control. Therefore, I decided to inherit the control from control instead of webcontrol, the inamingcontainer flag interface is also implemented to implement the uniqueness control of the sub-Control name.

In order to make the uploaded file name support the validator control, the validationpropertyattribute is added and pointed to filename. In order to make the client verification work normally, Asp. net requires that there must be an input form-based element named by the Control ID on the client. Therefore, when I create a composite control, I add an inputhidden control to support client verification. This inputhidden. the value attribute contains the name of the currently uploaded file. the parsechildren (true) and persistchildren (false) attributes are added to support the internal attributes of the effective extension array. From the above analysis, I obtained the following upload statement.

[Defaultproperty ( " Filename " ),

Validationpropertyattribute ( " Filename " ),

Parsechildren ( True ),

Persistchildren ( False ),

Toolboxdata ( " <{0}: Upload runat = Server> </{0}: Upload> " )]

Public   Class Upload: system. Web. UI. Control, inamingcontainer


To increase the verification of the upload file type, I declare the extfilter attribute to save valid extension file names.

Extfilters # Region Extfilters

Private Stringitemcollection _ extfilters;

[

Notifyparentproperty ( True ),

Persistencemode (persistencemode. innerproperty ),

Designerserializationvisibility (designerserializationvisibility. content ),

]

Public Stringitemcollection extfilters

{

Get  

{

If (_ Extfilters = Null )

{

_ Extfilters =   New Stringitemcollection ();

If ( This . Istrackingviewstate)

_ Extfilters. trackviewstate ();

}

Return _ Extfilters;

}

}

# Endregion


Now all the main elements in the upload control have been analyzed clearly. next articleArticleI will start the specific development work

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.