Design for allowing one upload of multiple files in Asp.net 2.0

Source: Internet
Author: User
In Asp.net 2.0, you can combine DHTML to realize that after the user uploads a file, he can click "Continue upload", and the upload file box is added dynamically.
First, the front-end page
<Script language = "JavaScript" type = "text/JavaScript">
Function AddFile (max)
{
Var file = Document. getelementsbyname ("file ");
If (file. Length = 1 & file [0]. Disabled = true)
{
File [0]. Disabled = false;
Return;
}
If (file. Length <max)
{
VaR filebutton = '<br/> <input type = "file" size = "50" name = "file" class = "button"/> ';
Document. getelementbyid ('filelist'). insertadjacenthtml ("beforeend", filebutton );
}
}
</SCRIPT>
</Head>
<Body>
<Form ID = "form1" runat = "server" method = "Post" enctype = "multipart/form-Data">
<Table class = "text" border = "0" cellpadding = "3" bgcolor = "black" cellspacing = "1">
<Tr bgcolor = "white">
<TD> category: </TD>
& Lt; TD width = "90%" & gt;
<Asp: dropdownlist id = "ddlcategory" runat = "server" skinid = "ddlskin" width = "336px"> </ASP: dropdownlist>
</TD>
</Tr>
<Tr bgcolor = "white">
<TD valign = "TOP"> select a photo: </TD>
& Lt; TD width = "90%" & gt;
<Table border = "0" cellpadding = "0" cellspacing = "0">
<Tr>
<TD valign = "TOP">
<P id = "filelist"> <input type = "file" Disabled = "disabled" size = "50" name = "file" class = "button"/> </P>
</TD>
<TD valign = "TOP"> <input type = "button" value = 'Add a photo 'class = "button" onclick = "AddFile (<% = maxphotocount %>) "/> <font color =" red "> (a maximum of <% = maxphotocount %> photos can be uploaded.) </font> <br/> click this button to add an upload photo button. If the file name or content is empty, do not upload the photo. </TD>
</Tr>
</Table>
</TD>
</Tr>
<Tr bgcolor = "white">
<TD> & nbsp; </TD>
& Lt; TD width = "90%" & gt;
<Asp: button id = "btncommit" runat = "server" text = "Submit" skinid = "btnskin" width = "100px" onclick = "btncommit_click"/> & nbsp; <asp: Label id = "lbmessage" runat = "server" cssclass = "text" forecolor = "red"> </ASP: Label>
</TD>
</Tr>
</Table>
A JavaScript code is used to dynamically generate multiple Upload File boxes. Note that a DHTML function insertadjacenthtml is used as follows:

Add HTML content (insertadjacenthtml and insertadjacenttext)
DHTML provides two methods to add, insertadjacenthtml and insertadjacenttext.
Insertadjacenthtml: insert the HTML Tag statement at the specified place.
Prototype: insertadjacenthtml (swhere, stext)
Parameters:
Swhere: specifies where HTML tag statements are inserted. Four values can be used:
1. beforebegin: before the start of the tag
2. afterbegin: inserted after the tag starts Marking
3. beforeend: insert to the end tag of the tag.
4. afterend: insert to the end tag of the tag.
Stext: content to be inserted
For example, VAR shtml = "<input type = button onclick =" + "go2 ()" + "value = 'click me'> <br>"
VaR sscript = '<SCRIPT defer>'
Sscript = sscript + 'function go2 () {alert ("hello from inserted script .")}'
Sscript = sscript + '</script' + '> ';
Scriptdiv. insertadjacenthtml ("afterbegin", shtml + sscript );
Add a line to the html body:
<Div id = "scriptdiv"> </div>
Eventually:
<Div id = "scriptdiv">
<Input type = button onclick = go2 () value = 'click me'> <br>
<SCRIPT defer>
Function go2 () {alert ("hello from inserted sctipt .")}'
</SCRIPT>
</Div>
The insertadjacenttext method is similar to the insertadjacenthtml method, but only plain text can be inserted and the parameters are the same.

Next we can use the for loop to process it.
Protected void btncommit_click (Object sender, eventargs E)
{
If (ddlcategory. selectedindex <= 0) return;
/// Obtain the list of uploaded files
Httpfilecollection filelist = httpcontext. Current. Request. files;
If (filelist = NULL) return;
Album album = new album ();
Try
{// Upload each file in the file list
For (INT I = 0; I <filelist. Count; I ++)
{// Obtain the currently uploaded file
Httppostedfile postedfile = filelist [I];
If (postedfile = NULL) continue;
/// Obtain the name of the uploaded file
String filename = path. getfilenamewithoutextension (postedfile. filename );
String extension = path. getextension (postedfile. filename );
If (string. isnullorempty (Extension) = true) continue;
/// Determine whether the file is legal
Bool isallow = false;
Foreach (string ext in ajaxalbumsystem. allowphotofilelist)
{
If (EXT = extension. tolower ())
{
Isallow = true;
Break;
}
}
If (isallow = false) continue;
/// Obtain the time-based file name
String timefilename = ajaxalbumsystem. createdatetimestring ();
/// Obtain the URL stored in the database
String url = "photoes/" + timefilename + extension;
/// Obtain the full path
String fullpath = server. mappath (URL );
/// Upload a file
Postedfile. saveas (fullpath );
/// Add a file to the database
Album. addphoto (filename, URL, postedfile. contenttype, postedfile. contentlength,
Int32.parse (ddlcategory. selectedvalue ));
}
}
Catch (exception ex)
{// Display the upload operation failure message
Lbmessage. Text = "Upload File error. Error cause:" + ex. message;
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.