The input (File) control is mainly used to upload files to the server. The following example shows how to use the input (File) control:
Open. NET and drag an input (File) control on the page, right-click it, select "run as a server control", and drag a button and an image
Add the following to the button event:Code:
Protected void button#click (Object sender, eventargs E)
{
String fullname = This. file1.postedfile. filename; // obtain the complete path and file name of the file on the client.
String name = fullname. substring (fullname. lastindexof ("\") + 1); // The string truncation function retrieves the file name.
String type = Name. substring (name. lastindexof (".") + 1); // The string truncation function gets the file suffix
If (type = "jpg" | type = "BMP" | type = "GIF") // you can determine whether the image is an image.
{
This. file1.postedfile. saveas (server. mappath ("up") + "\" + name); // upload to the server up directory
This. image1.imageurl = "up/" + name; // Image Display
}
Else
{
Response. Write ("<script language = 'javascript '> alert ('the format of the image you selected is incorrect! ') </SCRIPT> ");
}
}
The test passed on. net2005. If it is in another format, you can change the corresponding suffix. To prevent duplicate names, you can use a random function to rename the name variable together with the date!
========================================================== ======================================
Appendix: indexof () substring () function usage:
Indexof (): locates the character and string from the front to the back in the string. All return values are absolute positions in the string. If it is null, it is-1.
String test = "asdfjsdfjgkfasdsfsgfhgjgfjgdddd ";
Test. indexof ('D') = 2 // locate the location where D appears for the first time.
Test. indexof ('D', 1) = 2 // locate the first position where D appears from the third string
Test. indexof ('D', 5, 2) = 6 // locate D from the front to the back and query it from 5th bits. Check for 2 bits, that is, from 5th bits to 7th bits;
Lastindexof (): locates the character and string from the back and forward in the string ;,
The usage is exactly the same as that of indexof.
The following describes indexofany | lastindexofany.
They accept the character array as the variable element. The other methods are the same as above, and return the subscript location of any character in the array.
As follows:
Char [] bbv = {'s ', 'C',' B '};
String abc = "acsdfgdfgchacstcsad ";
Response. Write (ABC. indexofany (bbv) = 1
Response. Write (ABC. indexofany (bbv, 5) = 9
Response. Write (ABC. indexofany (bbv, 5, 3) = 9
Lastindexofany is the same as above.
========================================================== ======================================
Substring () Usage
String A = "aadsfdjkfgklfdglfd"
A. substring (5) // capture all strings after the fifth digit
A. substring (0th) // intercept all strings from 5th
........................................... End ........