File Upload instance, from http://www.chinabs.net "> China BS Network
<% @ Import namespace = "system. Io" %>
<% @ Page Language = "C #" DEBUG = "true" %>
<HTML>
<Head>
<Title> Upload File, http://www.chinabs.net </title>
<Script language = "C #" runat = "server">
// This method is called when the "Upload" button ID pressed
Public void uploadfile (Object sender, eventargs E)
{
// Check that the uploaded file is not empty
If (myfile. postedfile! = NULL)
{
String Nam = myfile. postedfile. filename;
// Obtain the index of the last "." In the file name (including the path)
Int I = Nam. lastindexof (".");
// Get the file extension
String newext = Nam. substring (I );
// Here, I automatically name the File Based on the date and file size to ensure that the file name is not repeated
Datetime now = datetime. now;
String newname = now. dayofyear. tostring () + myfile. postedfile. contentlength. tostring ();
// 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 "\"
Myfile. postedfile. saveas (server. mappath ("\ upload \" + newname + newext ));
// Obtain the relevant attributes of the file: file name, file type, and file size.
Fname. Text = myfile. postedfile. filename;
Fenc. Text = myfile. postedfile. contenttype;
Fsize. Text = myfile. postedfile. contentlength. tostring ();
}
}
</SCRIPT>
</Head>
<Body>
<Center>
<H3> File Upload instance, from <a href = "http://www.chinabs.net"> BS Network </a>
<Form ID = "uploderform" method = "Post" Action = "fileupload. aspx" enctype = "multipart/form-Data" runat = "server">
<Table border = "1" cellspacing = "2" cellpadding = "2">
<Tr> <TD> <H5> select the file to be uploaded: </H5> </TD </tr>
<Tr>
<TD>
<Input type = "file" id = "myfile" runat = "server" name = "myfile">
</TD>
</Tr>
<Tr> <TD>
<Input type = "button" value = "Upload" onserverclick = "uploadfile" runat = "server" id = "button1" name = "button1">
</TD> </tr>
</Table>
</Form>
<Br>
<Br>
<Table border = "1" cellspacing = "2">
<Tr> <TD> <B> documents </B> </TD>
<TD> & nbsp; </TD>
</Tr>
<Tr>
<TD> file name: </TD>
<TD> <asp: Label id = "fname" text = "" runat = "server"/> </TD> </tr>
<Tr>
<TD> file type: </TD>
<TD> <asp: Label id = "fenc" runat = "server"/> </TD> </tr>
<Tr>
<TD> file size: (in bytes) </TD>
<TD> <asp: Label id = "fsize" runat = "server"/> </TD> </tr>
</Table>
<Br>
<Br>
<Br>
</Center>
</Body>
</Htm>