Use the "htmlinputfile" control to upload files.
Set "enctype =" multipart/form-Data "in the form label ""
For example, <Form ID = "form1" enctype = "multipart/form-Data" runat = "server">
<Input type = "file" id = "file1" runat = "server"/>
// Place a button
</Form>
BackgroundCodeIs:
Click to processProgramIs:
// check that the uploaded file is not empty
If (file1.postedfile! = NULL)
{< br> string Nam = file1.postedfile. filename;
// get the last ". "index
int I = Nam. lastindexof (". ");
// get the file extension
string newext = Nam. substring (I);
// here, the file name is automatically named Based on the date and file size to ensure that the file name is not repeated
datetime now = datetime. now;
string newname = now. ticks. tostring ();
// save the file to your desired directory, which is the upload directory under the IIS root directory. you can change.
// Note: Server is used here. mappath () gets the absolute directory of the current file. in Asp.net, "" must be replaced with ""
file1.postedfile. saveas (server. mappath (".. /upload/"+ newname + newext);
// Obtain the relevant attributes of the file: file name, file type, and file size.
// Fname. Text = file1.postedfile. filename;
// Fenc. Text = file1.postedfile. contenttype; format: "image/JPEG", "image/GIF", etc.
// Fsize. Text = file1.postedfile. contentlength. tostring ();
}
You can use the file name extension to determine the file type.