ASP. NET Image Upload instance (with source code ),

Source: Internet
Author: User

ASP. NET Image Upload instance (with source code ),

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

<% @ 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 "> <Head runat =" server "> <title> upload and display images </title> <style type =" text/css ">. pic_text {color: Red ;}. pic_label {color: Gray; margin-top: 5px; margin-bottom: 5px ;}. pic_image {margin: 5px ;} </style> 


2. Background code

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

<? Xml version = "1.0" encoding = "UTF-8"?> <! -- How to configure ASP. NET application details --> <configuration> <system. web> <compilation debug = "true" targetFramework = "4.0"/> 

Automatic ASP. NET Image Upload and partial refresh displaySource code download.

I hope you will like this article.

Related Article

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.