Asp. NET study notes (4)--Upload images

Source: Internet
Author: User

Description (2017-10-8-23:03:43):

1. Behind the content are some assorted, bits and pieces, before all directly skipped, but in fact, it is very important, this time to re-learn to be serious and really knocked over.

2. Tomorrow at noon 9th to return to Beijing, tonight with the introduction of the sister chatted for one hours, hey!

3. Add a watermark to the picture!

Index.html

1<! DOCTYPE HTML Public"-//W3C//DTD XHTML 1.0 transitional//en" "HTTP://WWW.W3.ORG/TR/XHTML1/DTD/XHTML1-TRANSITIONAL.DTD">2"http://www.w3.org/1999/xhtml">34<title></title>56<body>7<form action="index.ashx"Method="Post"Enctype="Multipart/form-data">8<input type="file"Name="file"/>9<input type="text"Name="txt"/>Ten<input type="Submit"Value="Upload"/> One</form> A</body> -

Index.ashx

1 usingSystem;2 usingSystem.Collections.Generic;3 usingSystem.Linq;4 usingsystem.web;5 usingSystem.IO;6 7 namespace_02_ image Upload8 {9     /// <summary>Ten     ///Summary description of index One     /// </summary> A      Public classIndex:ihttphandler -     { -  the          Public voidProcessRequest (HttpContext context) -         { -Context. Response.ContentType ="text/html"; -             //receiving file Data +Httppostedfile file = context. request.files["file"]; -             if(File = =NULL) +             { AContext. Response.Write ("No pictures uploaded! "); at             } -             Else -             { -  -                 //string fileName = Path.getfilename (file. FileName); -                 //Plus globally unique identification to prevent picture duplication inGuid g =Guid.NewGuid (); -                 stringFileName =Guid.NewGuid (). ToString (); to                 //Get extension +                 stringFileext =path.getextension (file. FileName); -                 //relative Path the                 stringFilePath ="image/"+ DateTime.Now.Year +"/"+ DateTime.Now.Month +"/"+ DateTime.Now.Day +"/"; *                 //Absolute Path $                 //Create a folder, named after month and dayPanax Notoginseng                 stringFilerealpath = context. Request.mappath ("image/") + DateTime.Now.Year +"/"+ DateTime.Now.Month +"/"+ DateTime.Now.Day +"/"; - directory.createdirectory (filePath); the                 //determine file types to prevent uploading viruses +                 if(Fileext = =". jpg") A                 { theFile. SaveAs (Filerealpath + fileName +fileext); +                     //The picture path of this place needs to be relative path, otherwise it will not show -Context. Response.Write (""+ FilePath + fileName + fileext +"'/></body>"); $                 } $  -             } -         } the  -          Public BOOLisreusableWuyi         { the             Get -             { Wu                 return false; -             } About         } $     } -}

Asp. NET study notes (4)--Upload images

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.