原文地址为:
【ASP.NET 基础】图片上传和显示
由于需要图片上传的功能,所以花了一些时间网上找相关资料终于搞定,效果图如下:
下面的是解决方案截图和上传的图片截图:
下面是代码:
1.界面代码
UploadPic.aspx
- <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="UploadPic.aspx.cs" Inherits="Pic_Try.UploadPic" %><br /><br /><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><br /><br /><html xmlns="http://www.w3.org/1999/xhtml"><br /><head runat="server"><br /> <title>图片上传和显示</title><br /> <style type="text/css"><br /> .pic_text{ color:Red;}<br /> .pic_label { color:Gray; margin-top:5px; margin-bottom:5px;}<br /> .pic_image { margin:5px;}<br /></style><br /></head><br /><body><br /> <form id="form1" runat="server"><br /> <div class="pic_image"><asp:Image ID="pic" runat="server" /></div><br /> <div><asp:FileUpload ID="pic_upload" runat="server" /><asp:Label ID="lbl_pic" runat="server" class="pic_text"></asp:Label></div><br /> <div class="pic_label">上传图片格式为.jpg, .gif, .bmp,.png,图片大小不得超过8M</div><br /> <div><asp:Button ID="btn_upload" runat="server" Text="上传" οnclick="btn_upload_Click"/></div><br /> </form><br /><br /></body><br /></html>
复制代码
2.后台代码
UploadPic.aspx.cs
- using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Web;<br />using System.Web.UI;<br />using System.Web.UI.WebControls;<br />using System.IO;<br />using System.Security.Cryptography;<br />using System.Web.Security;<br /><br />namespace Pic_Try<br />{<br />public partial class UploadPic : System.Web.UI.Page<br /> {<br />protected void Page_Load(object sender, EventArgs e)<br /> {<br /><br /> }<br /><br />protected void btn_upload_Click(object sender, EventArgs e)<br /> {<br /> Boolean fileOk = false;<br />if (pic_upload.HasFile)//验证是否包含文件<br /> {<br />//取得文件的扩展名,并转换成小写<br />string fileExtension = Path.GetExtension(pic_upload.FileName).ToLower();<br />//验证上传文件是否图片格式<br /> fileOk = IsImage(fileExtension);<br /><br />if (fileOk)<br /> {<br />//对上传文件的大小进行检测,限定文件最大不超过8M<br />if (pic_upload.PostedFile.ContentLength < 8192000)<br /> {<br />string filepath = "/images/";<br />if (Directory.Exists(Server.MapPath(filepath)) == false)//如果不存在就创建file文件夹<br /> {<br /> Directory.CreateDirectory(Server.MapPath(filepath));<br /> }<br />string virpath = filepath + CreatePasswordHash(pic_upload.FileName, 4) + fileExtension;//这是存到服务器上的虚拟路径<br />string mappath = Server.MapPath(virpath);//转换成服务器上的物理路径<br /> pic_upload.PostedFile.SaveAs(mappath);//保存图片<br />//显示图片<br /> pic.ImageUrl = virpath;<br />//清空提示<br /> lbl_pic.Text = "";<br /> }<br />else {<br /> pic.ImageUrl = "";<br /> lbl_pic.Text = "文件大小超出8M!请重新选择!";<br /> }<br /> }<br />else {<br /> pic.ImageUrl = "";<br /> lbl_pic.Text = "要上传的文件类型不对!请重新选择!";<br /> }<br /> }<br />else<br /> {<br /> pic.ImageUrl = "";<br /> lbl_pic.Text = "请选择要上传的图片!";<br /> }<br /> }<br /><br />/// <summary><br />/// 验证是否指定的图片格式<br />/// </summary><br />/// <param name="str"></param><br />/// <returns></returns><br />public bool IsImage(string str) {<br />bool isimage = false;<br />string thestr = str.ToLower();<br />//限定只能上传jpg和gif图片<br />string[] allowExtension = { ".jpg", ".gif", ".bmp",".png" };<br />//对上传的文件的类型进行一个个匹对<br />for (int i = 0; i < allowExtension.Length; i++)<br /> {<br />if (thestr == allowExtension[i])<br /> {<br /> isimage = true;<br />break;<br /> }<br /> }<br />return isimage;<br /> }<br /><br />/// <summary><br />/// 创建一个指定长度的随机salt值<br />/// </summary><br />public string CreateSalt(int saltLenght)<br /> {<br />//生成一个加密的随机数<br /> RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();<br />byte[] buff = new byte[saltLenght];<br /> rng.GetBytes(buff);<br />//返回一个Base64随机数的字符串<br />return Convert.ToBase64String(buff);<br /> }<br /><br /><br />/// <summary><br />/// 返回加密后的字符串<br />/// </summary><br />public string CreatePasswordHash(string pwd, int saltLenght)<br /> {<br />string strSalt = CreateSalt(saltLenght);<br />//把密码和Salt连起来<br />string saltAndPwd = String.Concat(pwd, strSalt);<br />//对密码进行哈希<br />string hashenPwd = FormsAuthentication.HashPasswordForStoringInConfigFile(saltAndPwd, "sha1");<br />//转为小写字符并截取前16个字符串<br /> hashenPwd = hashenPwd.ToLower().Substring(0, 16);<br />//返回哈希后的值<br />return hashenPwd;<br /> }<br /> }<br />}
复制代码
3.最后防止上传大文件图片时报错,配置文件添加配置
Web.config
- <?xml version="1.0" encoding="utf-8"?><br /><br /><!--<br /> 有关如何配置 ASP.NET 应用程序的详细消息,请访问<br /> http://go.microsoft.com/fwlink/?LinkId=169433<br /> --><br /><br /><configuration><br /> <system.web><br /> <compilation debug="true" targetFramework="4.0" /><br /> <httpRuntime executionTimeout="240" maxRequestLength="8192000"/><br /> </system.web><br /><br /></configuration>
复制代码
源代码下载
转载请注明本文地址:
【ASP.NET 基础】图片上传和显示
来源:https://blog.csdn.net/dearbaba_8520/article/details/82827249 免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作! |