ASP. NET (C #) enables one-time upload of multiple images (multiple files)

Source: Internet
Author: User

<% @ Page Language = "C #" autoeventwireup = "true" codefile = "fileup. aspx. cs" inherits = "fileup" %>

<! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<HTML xmlns = "http://www.w3.org/1999/xhtml">
<Head runat = "server">
<Title> ASP. NET (C #) allows you to upload multiple images (multiple files) at a time. </title>

<SCRIPT type = "text/JavaScript">
VaR I = 1
Function AddFile ()
{

If (I <8)
{Var STR = '<br> <input type = "file" name = "file" runat = "server" style = "width: 200px"/> description: <input name = "text" type = "text" style = "width: 150px" maxlength = "20"/>'
Document. getelementbyid ('myfile'). insertadjacenthtml ("beforeend", STR)

}
Else
{
Alert ("you can upload up to 8 images at a time! ")
}
I ++
}
</SCRIPT>


</Head>
<Body>
<Form ID = "form1" runat = "server">
<Div>
<Table id = "Table1" align = "center" border = "0" cellpadding = "1" cellspacing = "1" class = "table">

<Tr>
<TD align = "center">
<Font color = "# 0000ff" face = "" size = "3"> <strong> upload images </strong> </font> </TD>
</Tr>
<Tr>
<TD align = "center" style = "width: 734px">
& Nbsp; </TD>
</Tr>
<Tr>
<TD align = "center" style = "width: 734px">
<Asp: Panel id = "panel5" runat = "server">
& Nbsp; <Table width = "100%">
<Tr>
<TD align = "right" style = "width: 100px">
</TD>
<TD align = "Left" style = "width: 500px">
Note: you can click the Add image button to upload multiple images at a time. You can enter a description of up to 20 words for each image. The size of a single image cannot exceed 1024 kb </TD>
</Tr>
<Tr>
<TD align = "right" style = "width: 100px">
Select an image: <br/>
</TD>
<TD align = "Left" style = "width: 500px"> <p id = "myfile"> <input onclick = "AddFile () "type =" button "value =" add image "> </P> <br/>
<Input id = "file1" type = "file" name = "file" runat = "server" style = "width: 245px"/>
Description: <input name = "text" type = "text" style = "width: 150px" maxlength = "20"/>
</TD>
</Tr>

<Tr>
<TD align = "right" style = "width: 100px">
</TD>
<TD align = "Left" style = "width: 500px">
<Asp: button id = "btnupload" runat = "server" text = "Start upload" onclick = "btnupload_click"/>
</TD>
</Tr>
<Tr>
<TD align = "right" style = "width: 100px">
</TD>
<TD align = "Left" style = "width: 500px">
<Asp: Label id = "lblmessage" runat = "server" font-bold = "true" forecolor = "red"> </ASP: Label> </TD>
</Tr>
</Table>
</ASP: Panel>
& Nbsp;
</TD>
</Tr>
<Tr>
<TD align = "center" style = "width: 734px">
<Font face = ""> </font> <font face = ""> & nbsp; </font>
</TD>
</Tr>
</Table>

</Div>

</Form>
</Body>
</Html>

++ +++ CS File

Using system;
Using system. Data;
Using system. configuration;
Using system. collections;
Using system. Web;
Using system. Web. Security;
Using system. Web. UI;
Using system. Web. UI. webcontrols;
Using system. Web. UI. webcontrols. webparts;
Using system. Web. UI. htmlcontrols;

Using system. IO;

Public partial class fileup: system. Web. UI. Page
{
Protected void page_load (Object sender, eventargs E)
{

}
Protected void btnupload_click (Object sender, eventargs E)
{
Lblmessage. Text = "";
Lblmessage. Visible = false;
System. Web. httpfilecollection files = system. Web. httpcontext. Current. Request. files;
System. Text. stringbuilder strmsg = new system. Text. stringbuilder ("");
String [] RD = request. Form [1]. Split (','); // string array of the text box for obtaining the image description, which is the description of the corresponding image
// String albumid = ddlalbum. selectedvalue. Trim ();
Int ifile;
For (ifile = 0; ifile <files. Count; ifile ++)
{
If (files [ifile]. filename. length> 0)
{
System. Web. httppostedfile postedfile = files [ifile];
If (postedfile. contentlength/1024> 1024) // a single file cannot exceed 1024 KB
{
Strmsg. append (path. getfilename (postedfile. filename) + "--- cannot exceed 1024 K <br> ");
Break;
}
String Fex = path. getextension (postedfile. filename );
If (FEX! = ". Jpg" & Fex! = ". Jpg" & Fex! = ". GIF" & Fex! = ". GIF ")
{
Strmsg. append (path. getfilename (postedfile. filename) + "--- incorrect image format, only jpg or GIF <br> ");
Break;
}
}
}
If (strmsg. Length <= 0) // The image size and format are correct.
{
// Create an image library directory
String dirpath = server. mappath ("51 aspx ");

If (directory. exists (dirpath) = false)
{
Directory. createdirectory (dirpath );
}
Random RO = new random ();
Int name = 1;
For (INT I = 0; I <files. Count; I ++)
{
System. Web. httppostedfile myfile = files [I];
String filename = "";
String fileextention = "";
Filename = system. Io. Path. getfilename (myfile. filename );
String STRO = Ro. Next (100,100 1000000). tostring () + name. tostring (); // generates a random number for the newly named image.
String newname = datetime. Now. Minute. tostring () + datetime. Now. Second. tostring () + datetime. Now. millisecond. tostring () + STRO;
If (filename. length> 0) // only files are available for upload and then saved to the database.
{
Fileextention = system. Io. Path. getextension (myfile. filename );

String ppath = dirpath + @ "\" + newname + fileextention;
Myfile. saveas (ppath );

}

Name = Name + 1; // The variable used to rename the rule

}

Response. Write ("<SCRIPT> alert ('Congratulations, the image is uploaded successfully! ') </SCRIPT> ");
}
Else
{
Lblmessage. Text = strmsg. tostring ();
Lblmessage. Visible = true;
}
}
}

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.