Note: I have read the tutorial of Dai Ge and raised a few questions, which can be discussed below:
Step 1:Create a website in the following language: Visual C #. A file named default. aspx and default. aspx. CS is generated;
Step 2:Create an up folder and an upfile folder in the same directory of the default. aspx file to store the uploaded files;
Step 3:In the design view of the default. aspx. aspx file, drag the server control 4 from the toolbox,
They are: fileupload, button, image, and label;
Step 4:Arrange various controls according to your preferences, and expand the display areas of the image and label controls;
Step 5:Change the text attribute of the label control to a null value, and change the text attribute of the button control to "Upload ";
Step 6:Change the ID attribute of the four controls to: upfileupload1, upbutton1, upimage1, and uplabel1;
That is, with "up", we need to develop the habit of ID change;
Step 7:In the design view of the default. aspx. aspx file, double-click the button control to go to programming,CodeAs follows:
Protected Void Upbutton1_click ( Object Sender, eventargs E)
{
String Name = Upfileupload1.filename; // Get the name of the uploaded folder
String Size = Upfileupload1.postedfile. contentlength. tostring (); // Obtains the size of the uploaded folder.
String Type = Name. substring (name. lastindexof ( " . " ) + 1 ); // Get the file suffix
String Ipath = Server. mappath ( " Up " ) + " \\ " + Name; // Get the actual path of folder upload
String Fpath = Server. mappath ( " Upfile " ) + " \\ " + Name; // Get the actual path of folder upload
String Wpath = " Up \\ " + Name; // Virtual Path written to the database
// Next we need to determine the file format
If (Type = " JPG " | Type = " GIF " | Type = " BMP " | Type = " PNG " )
{
Upimage1.visible = True ;
Upfileupload1.saveas (ipath ); // Save the file to the path
Upimage1.imageurl = Wpath; // Display the image
Uplabel1.text = " The file name you passed is: " + Name + " <Br> the file size is: " + Size + " Byte <br> the file type is: " + Type + " <Br> the suffix is: " + Type + " <Br> the actual path is: " + Ipath + " <Br> the virtual path is: " + Wpath;
}
Else
{
Upimage1.visible = False ; // Hide the image control, otherwise it will show a small xx
Upfileupload1.saveas (fpath ); // Save the folder in another folder
Uplabel1.text = " The file name you passed is: " + Name + " <Br> the file size is: " + Size + " Byte <br> the file type is: " + Type + " <Br> the suffix is: " + Type + " <Br> the actual path is: " + Ipath + " <Br> the virtual path is: " + Wpath;
}
}
Step 8:Add the following code to page_load:
Protected Void Page_load ( Object Sender, eventargs E)
{
If ( ! This . Ispostback)
{
Upimage1.visible = False ; // Hide the image control, otherwise it will show a small xx
}
}
Step 9:OK. Start debugging.
For more information, see:
· Upload multiple files at the same time
· The file suffix is lower-case and can be uploaded, for example, JPG, GIF, BMP, and PNG files. However, if the file suffix is upper-case, it will not work.
· How can I solve the problem of file name duplication when there are too many files uploaded?
· How to prevent malicious attacks