Use the fileload control:
<Asp: fileupload id = "fileupload1" runat = "server" width = "217px"/> // fileload Control
<Asp: button id = "button1" runat = "server" onclick = "button#click" text = "Upload"/> upload button
Background code:
Protected void button#click (Object sender, eventargs e) {try {If (fileupload1.postedfile. filename = "") {alert ("select a file! ");} Else {string filepath = fileupload1.postedfile. filename; // If (! Isallowedextension (fileupload1) // {// This. lb_info.text = "the format of the uploaded file is incorrect! "; //} If (isallowedextension (fileupload1) = true) {string filename = filepath. substring (filepath. lastindexof ("\") + 1); filename = session ["stuid"]. tostring () + ". jpg "; string serverpath = server. mappath ("~ /Examer_images/") + filename; fileupload1.postedfile. saveas (serverpath); If (ispropersize (serverpath) {alert (" image uploaded successfully! ") ;}} Else {alert (" upload an image! ") ;}} Catch (exception ex) {alert (" Upload error! Cause: "+ ex. tostring ());}}
Determine whether the file is an image file:
Private bool isallowedextension (fileupload upfile) {string stroldfilepath = ""; string strextension = ""; string [] arrextension = {". GIF ",". jpg ",". BMP ",". PNG "}; If (upfile. postedfile. filename! = String. empty) {stroldfilepath = upfile. postedfile. filename; // obtain the complete file path strextension = stroldfilepath. substring (stroldfilepath. lastindexof (". "); // get the file extension, such :. JPG for (INT I = 0; I <arrextension. length; I ++) {If (strextension. equals (arrextension [I]) {return true ;}}return false ;}
Determine whether the image meets the specifications:
Private bool ispropersize (string filename) {system. drawing. image image2 = system. drawing. image. fromfile (filename); If (image2.width> 441) & (image2.height> 358) {image2.dispose (); response. write ("<script language = 'javascript '> alert ('width and height greater than 520 * 520px! ') </SCRIPT> "); system. Io. file. Delete (filename); // delete the file return false;} else return true ;}
Summary:
Obtain the selected image name: String filepath = fileupload1.postedfile. filename;
Obtain the name of the directory to be uploaded: String serverpath = server. mappath ("~ /Examer_images/") + filename; // upper-level directory of the Program Folder
The server obtains the following path rules:
1. server. mappath ("/")
Note: Obtain the location of the application root directory, such as c: \ Inetpub \ wwwroot \.
2. server. mappath ("./")
Note: Obtain the current directory of the page, which is equivalent to server. mappath ("").
3. server. mappath ("../")
Note: Obtain the parent directory of the page.
4. server. mappath ("~ /")
Note: Obtain the directory of the current application. If it is the root directory, it is the root directory. If it is a virtual directory, it is the location of the virtual directory, such as C: \ Inetpub \ wwwroot \ example \.
Eg:
String filename = server. mappath ("./") + @ "\ WEB. config ";
String filename = server. mappath ("./") + "/Web. config ";
String filename = server. mappath ("") + @ "\ WEB. config"
System. Io. file. Delete (filename); // delete an object