<% @ Page Language = "C #" DEBUG = "true" %>
<% @ Import namespace = "system. Io" %>
<Script language = "C #" runat = "server">
Public void uploadfile (Object sender, eventargs e ){
// Check that the uploaded file is not empty
If (inputfile. postedfile. contentlength> 0 ){
// Set the path to save the uploaded file
String strsavedir = "./upload /";
String strname = inputfile. postedfile. filename;
// Obtain the index of the last "." In the file name (including the path)
Int intext = strname. lastindexof (".");
// Get the file extension
String strext = strname. substring (intext );
// Here, I automatically name the File Based on the date and file size to ensure that the file name is not repeated
/*
Datetime datnow = datetime. now;
String strnewname = datnow. dayofyear. tostring () + inputfile. postedfile. contentlength. tostring () + strext ;*/
// Retrieve the last "\" index in the file name (including path)
Int intpath = strname. lastindexof ("\\");
// Get the file name (excluding the path)
String strnewname = strname. substring (intpath );
// Save the file to the directory you want. This is the upload directory under the IIS root directory. You can change it.
// Note: Here I use server. mappath () to retrieve the absolute directory of the current file. In Asp.net, "\" must be replaced "\"
Inputfile. postedfile. saveas (server. mappath (strsavedir + strnewname ));
// Obtain the relevant attributes of the file: file name, file type, and file size.
Labelupresult. Text = "Upload successful! ";
Labelfilename. Text = "File Source:" + strname;
Labelfileext. Text = "file type:" + inputfile. postedfile. contenttype + "(" + strext + ")";
Labelfilesize. Text = "file size:" + (inputfile. postedfile. contentlength/1024). tostring () + "K byte (s )";
} Else {
Labelupresult. Text = "select the file you want to upload! ";
Labelfilename. Text = "";
Labelfileext. Text = "";
Labelfilesize. Text = "";
}
}
</SCRIPT>
<HTML>
<Head>
<Title> upload a file </title>
</Head>
<Body>
<Div align = "center">
<Table width = "100%" border = "0" cellpadding = "0" cellspacing = "0" style = "border-collapse: collapse "bordercolor =" # eeeeee "id =" autonumber1 ">
<Form ID = "formfile" method = "Post" Action = "" enctype = "multipart/form-Data" runat = "server">
<Tr>
<TD align = "center">
<Input type = "file" id = "inputfile" name = "inputfile" runat = "server" size = "64">
</TD>
</Tr>
<Tr>
<TD align = "center">
<Input type = "button" value = "Upload" onserverclick = "uploadfile" id = "button1" name = "button1" runat = "server">
</TD>
</Tr>
<Tr>
<TD align = "center">
<Asp: Label id = "labelupresult" runat = "server" text = "" font-bold = "true" forecolor = "# ff0000"/> <br>
<Asp: Label id = "labelfilename" runat = "server" text = "" font-bold = "true" forecolor = "# ff0000"/> <br>
<Asp: Label id = "labelfileext" runat = "server" text = "" font-bold = "true" forecolor = "# ff0000"/> <br>
<Asp: Label id = "labelfilesize" runat = "server" text = "" font-bold = "true" forecolor = "# ff0000"/> <br>
</TD>
</Tr>
</Form>
</Table>
</Div>
</Body>
</Html>