Because the image upload function is required, it takes some time to find relevant information on the Internet, as shown below:
The following are the solutions and uploaded images:
The following code is used:
1. interface code
Uploadpic. aspx
<% @ Page Language = "C #" autoeventwireup = "true" codebehind = "uploadpic. aspx. cs" inherits = "pic_try.uploadpic" %> <! Doctype HTML public "-// W3C // dtd xhtml 1.0 transitional // en" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <HTML xmlns = "http://www.w3.org/1999/xhtml">
2. Background code
Uploadpic. aspx. CS
Using system; using system. collections. generic; using system. LINQ; using system. web; using system. web. ui; using system. web. UI. webcontrols; using system. io; using system. security. cryptography; using system. web. security; namespace pic_try {public partial class uploadpic: system. web. UI. page {protected void page_load (Object sender, eventargs e) {} protected void btn_upload_click (Object sender, eventargs e ){ Boolean fileok = false; If (pic_upload.hasfile) // verify whether the file contains the file {// obtain the file extension and convert it to lowercase string fileextension = path. getextension (pic_upload.filename ). tolower (); // verify whether the image format of the uploaded file is fileok = isimage (fileextension); If (fileok) {// check the size of the uploaded file, the limit file cannot exceed 8 Mb if (pic_upload.postedfile.contentlength <8192000) {string filepath = "/images/"; if (directory. exists (server. mappath (filepath) = false) // create a file folder if it does not exist {Directory. createdirectory (server. mappath (filepath);} string virpath = filepath + createpasswordhash (pic_upload.filename, 4) + fileextension; // This is the virtual path stored on the server string mappath = server. mappath (virpath); // converts it to the physical path pic_upload.postedfile.saveas (mappath) on the server; // saves the image // displays the image pic. imageurl = virpath; // The lbl_pic.text = "";} else {pic. imageurl = ""; lbl_pic.text = "the file size exceeds 8 Mb! Please select again! ";}} Else {pic. imageurl =" "; lbl_pic.text =" the type of the file to be uploaded is incorrect! Please select again! ";}} Else {pic. imageurl =" "; lbl_pic.text =" select the image to upload! ";}} /// <Summary> /// verify whether the specified image format is used. /// </Summary> /// <Param name = "str"> </param> /// <returns> </returns> Public bool isimage (string Str) {bool isimage = false; string thestr = Str. tolower (); // only jpg and GIF image string [] allowextension = {". jpg ",". GIF ",". BMP ",". PNG "}; // perform a pair of for (INT I = 0; I <allowextension. length; I ++) {If (thestr = allowextension [I]) {isimage = true; break ;}} return isimage ;} /// <summary> /// create a random salt value of the specified length /// </Summary> Public String createsalt (INT saltlenght) {// generate an encrypted random number rngcryptoserviceprovider RNG = new rngcryptoserviceprovider (); byte [] buff = new byte [saltlenght]; RNG. getbytes (buff); // returns the return convert string of a base64 random number. tobase64string (buff);} // <summary> // return the encrypted string /// </Summary> Public String createpasswordhash (string PWD, int saltlenght) {string strsalt = createsalt (saltlenght); // connect the password to salt string saltandpwd = string. concat (PWD, strsalt); // hash the password string hashenpwd = formsauthentication. hashpasswordforstoringinconfigfile (saltandpwd, "sha1"); // convert it to lowercase characters and extract the first 16 strings hashenpwd = hashenpwd. tolower (). substring (0, 16); // return the hash value return hashenpwd ;}}}
3. Finally, to prevent the upload of large file images from returning an error, add configuration in the configuration file
Web. config
<? XML version = "1.0" encoding = "UTF-8"?> <! -- Access the http://go.microsoft.com/fwlink/ for detailed messages about how to configure ASP. NET applications? Linkid = 169433 --> <configuration> <system. web> <compilation DEBUG = "true" targetframework = "4.0"/>
Source code download