Asp. NET Picture upload example (source code) _ Practical Skills

Source: Internet
Author: User

Because of the need to upload the image function, so spent some time online to find relevant information finally finished, the effect of the figure is as follows:

The following is a screenshot of the solution and upload pictures:

Here's the code:
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 ">  


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, Event
  Args e) {} protected void Btn_upload_click (object sender, EventArgs e) {Boolean fileok = false; if (pic_upload. HasFile)//Verify that the file {//Gets the extension of the file and is converted to a lowercase string fileextension = Path.getextension (pic_upload. FileName).
  ToLower ();

  Verify that the upload file is in picture format FileOK = Isimage (fileextension); if (FileOK) {//the size of the uploaded file is detected, the maximum limit file is no more than 8M if pic_upload.
   Postedfile.contentlength < 8192000) {string filepath = "/images/"; if (Directory.Exists (Server.MapPath (filepath)) = = false)//create file Folder {Directory.CreateDirectory if not present (Server.mappat
   H (filepath)); String Virpath = filepath + createpasswordhash (pic_upload. FileName, 4) +fileextension;//This is a virtual path that is saved to the server, string MapPath = Server.MapPath (Virpath), or converted to a physical path pic_upload on the server. Postedfile.saveas (MapPath);//Save picture//display pic.
   IMAGEURL = Virpath; Empty prompt lbl_pic.
   Text = ""; else {pic.
   IMAGEURL = ""; Lbl_pic. Text = "File size exceeds 8m! Please choose again!
   "; } else {pic.
   IMAGEURL = ""; Lbl_pic. Text = "The file type to upload is not correct!" Please choose again!
  "; } else {pic.
  IMAGEURL = ""; Lbl_pic. Text = "Please select the picture you want to upload!"
  "; }///<summary>///Verify that the specified picture format///</summary>///<param name= "str" ></param>///<ret
  Urns></returns> public bool Isimage (string str) {bool isimage = false; string thestr = str.
  ToLower ();
  Limit upload jpg and gif pictures string[] allowextension = {". jpg", ". gif", ". bmp", ". png"}; For the type of uploaded file for (int i = 0; i < allowextension.length i++) {if (Thestr = = Allowextension[i]) {Isim
   Age = true;
  Break
 } return isimage; ///<summary>///creates a random salt value of a 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 string return convert.tobase64string (buff) of a Base64 random number; ///<summary>///Returns the encrypted string///</summary> public string Createpasswordhash (string pwd, int Saltlengh
  T) {string strsalt = CreateSalt (saltlenght);
  Connect the code to the salt. String saltandpwd = String.Concat (pwd, strsalt);
  Hash the password string hashenpwd = FormsAuthentication.HashPasswordForStoringInConfigFile (saltandpwd, "SHA1"); Switch to lowercase characters and intercept the first 16 strings hashenpwd = Hashenpwd.tolower ().
  Substring (0, 16);
 Returns the hash value return hashenpwd;

 }
 }
}

3. Finally prevent uploading large files picture times wrong, configuration file add configuration

<?xml version= "1.0" encoding= "Utf-8"?>

<!--
 How to configure the ASP.net application details
 -->

< configuration>
 <system.web>
 <compilation debug= "true" targetframework= "4.0"/>
 < HttpRuntime executiontimeout= "maxrequestlength=" "8192000"/>
 </system.web>

</ Configuration>

ASP. NET picture automatic upload and local refresh display source download.

I hope you enjoy this 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.