栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > asp

ASP.NET图片上传实例(附源码)

asp 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

ASP.NET图片上传实例(附源码)

由于需要图片上传的功能,所以花了一些时间网上找相关资料终于搞定,效果图如下:

下面的是解决方案截图和上传的图片截图:

下面是代码:
1.界面代码

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPic.aspx.cs" Inherits="Pic_Try.UploadPic" %>





 图片上传和显示
 
 .pic_text{ color:Red;}
 .pic_label { color:Gray; margin-top:5px; margin-bottom:5px;}
 .pic_image { margin:5px;}
 


 
 



2.后台代码

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)//验证是否包含文件
  {
  //取得文件的扩展名,并转换成小写
  string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();
  //验证上传文件是否图片格式
  fileOk = IsImage(fileExtension);

  if (fileOk)
  {
   //对上传文件的大小进行检测,限定文件最大不超过8M
   if (pic_upload.PostedFile.ContentLength < 8192000)
   {
   string filepath = "/images/";
   if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹
   {
    Directory.CreateDirectory(Server.MapPath(filepath));
   }
   string virpath = filepath + CreatePasswordHash(pic_upload.FileName, 4) + fileExtension;//这是存到服务器上的虚拟路径
   string mappath = Server.MapPath(virpath);//转换成服务器上的物理路径
   pic_upload.PostedFile.SaveAs(mappath);//保存图片
   //显示图片
   pic.ImageUrl = virpath;
   //清空提示
   lbl_pic.Text = "";
   }
   else {
   pic.ImageUrl = "";
   lbl_pic.Text = "文件大小超出8M!请重新选择!";
   }
  }
  else {
   pic.ImageUrl = "";
   lbl_pic.Text = "要上传的文件类型不对!请重新选择!";
  }
  }
  else
  {
  pic.ImageUrl = "";
  lbl_pic.Text = "请选择要上传的图片!";
  }
 }

 /// 
 /// 验证是否指定的图片格式
 /// 
 /// 
 /// 
 public bool IsImage(string str) {
  bool isimage = false;
  string thestr = str.ToLower();
  //限定只能上传jpg和gif图片
  string[] allowExtension = { ".jpg", ".gif", ".bmp",".png" };
  //对上传的文件的类型进行一个个匹对
  for (int i = 0; i < allowExtension.Length; i++)
  {
  if (thestr == allowExtension[i])
  {
   isimage = true;
   break;
  }
  }
  return isimage;
 }

 /// 
 /// 创建一个指定长度的随机salt值
 /// 
 public string CreateSalt(int saltLenght)
 {
  //生成一个加密的随机数
  RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
  byte[] buff = new byte[saltLenght];
  rng.GetBytes(buff);
  //返回一个base64随机数的字符串
  return Convert.Tobase64String(buff);
 }

 
 /// 
 /// 返回加密后的字符串
 /// 
 public string CreatePasswordHash(string pwd, int saltLenght)
 {
  string strSalt = CreateSalt(saltLenght);
  //把密码和Salt连起来
  string saltAndPwd = String.Concat(pwd, strSalt);
  //对密码进行哈希
  string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");
  //转为小写字符并截取前16个字符串
  hashenPwd = hashenPwd.ToLower().Substring(0, 16);
  //返回哈希后的值
  return hashenPwd;
 }
 }
}

3.最后防止上传大文件图片时报错,配置文件添加配置






 
 
 
 


ASP.NET图片自动上传和局部刷新显示的源码下载。

希望大家喜欢这篇文章。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/56624.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号