using System;using System.Collections.Generic;using System.linq;using System.Web;using System.Web.UI;using System.Web.UI.WebControls;using System.Security.Cryptography;using System.Web.Security;using System.IO;using System.Data.sqlClIEnt;using System.Data;namespace Demo.Web{ public partial class show : System.Web.UI.Page { protected voID Page_Load(object sender,EventArgs e) { } public string SlIDeShow() { BLL.slIDe SlIDe = new BLL.slIDe(); DataSet sdr = SlIDe.GetList(5); string navshow = ""; if (sdr != null) //表格形式显示数据 { try { foreach (DaTarow mDr in sdr.tables[0].Rows) { navshow += "<tr>"; navshow += "<td><a href=‘" + mDr[3].ToString() + "‘ target=‘_blank‘><img src=‘" + mDr[4].ToString() + "‘ wIDth=‘100‘ /></a></td>"; navshow += "<td>" + mDr[1].ToString() + "</td>"; navshow += "<td>" + mDr[3].ToString() + "</td>"; navshow += "<td align=‘center‘>" + mDr[2].ToString() + "</td>"; navshow += "<td align=‘center‘><a href=‘editshow.aspx?ID=" + mDr[0].ToString() + "‘>编辑</a> | <a href=‘delshow.aspxID=1‘>删除</a></td>"; navshow += "</tr>"; } } catch (sqlException ex) { Response.Write(ex.ToString()); } finally { ; } } return navshow; } protected voID submit_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 = "上传图片成功!";//上传图片成功 //下面是添加图片的整体信息 string slIDename = SlIDename.Text; string slIDeimg = pic.ImageUrl; string slIDeurl = SlIDeUrl.Text; string slIDddescrition = SlIDeSort.Text; Model.slIDe ModeSlIDe = new Model.slIDe(); ModeSlIDe.Title = slIDename; ModeSlIDe.img = pic.ImageUrl; ModeSlIDe.url = slIDeurl; ModeSlIDe.descrition = slIDddescrition; BLL.slIDe SlIDe = new BLL.slIDe(); if (Convert.ToBoolean(SlIDe.Add(ModeSlIDe))) { Response.Write("<script>alert(‘完整的添加图片成功!‘)</script>"); } else { Response.Write("<script>alert(‘失败!‘)</script>"); } } else { pic.ImageUrl = ""; lbl_pic.Text = "文件大小超出8M!请重新选择!"; } } else { pic.ImageUrl = ""; lbl_pic.Text = "要上传的文件类型不对!请重新选择!"; } } else { pic.ImageUrl = ""; lbl_pic.Text = "请选择要上传的图片!"; } } /// <summary> /// 验证是否指定的图片格式 /// </summary> /// <param name="str"></param> /// <returns></returns> 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; } /// <summary> /// 创建一个指定长度的随机salt值 /// </summary> public string CreateSalt(int saltLenght) { //生成一个加密的随机数 RNGCryptoServiceProvIDer rng = new RNGCryptoServiceProvIDer(); byte[] buff = new byte[saltLenght]; rng.GetBytes(buff); //返回一个Base64随机数的字符串 return Convert.ToBase64String(buff); } /// <summary> /// 返回加密后的字符串 /// </summary> 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; } }}
HTML
@H_404_8@<%@ Page Language="C#" autoEventWireup="true" CodeBehind="show.aspx.cs" inherits="Demo.Web.show" %><!DOCTYPE aspx PUBliC "-//W3C//DTD Xaspx 1.0 Transitional//EN" "http://www.w3.org/TR/xaspx1/DTD/xaspx1-Transitional.dtd"><aspx xmlns="http://www.w3.org/1999/xaspx"><head runat="server"> <Meta http-equiv="Content-Type" content="text/aspx; charset=UTF-8"><Title>Douaspx 管理中心 - 首页幻灯广告 </Title><Meta name="copyright" content="Douco Design." /><link href="CSS/public.CSS" rel="stylesheet" type="text/CSS"><script type="text/JavaScript" src="Js/jquery.min.Js"></script><script type="text/JavaScript" src="Js/global.Js"></script><style> #div_pic { wIDth: 40%; height: 40%; margin-bottom: 5%; } #div_pic img { wIDth: 100%; height: auto; } .style2 { wIDth: 227px; } .style3 { wIDth: 112px; } .style5 { wIDth: 125px; } .style6 { wIDth: 138px; }</style></head><body> <form ID="form1" runat="server"><div ID="dcWrap"> <div ID="dchead"> <div ID="head"> <div class="logo"><a href="index.aspx"><img src="images/dclogo.gif" alt="logo"></a></div> <div class="nav"> <ul> <li class="M"><a href="JavaScript:voID(0);" class="topAdd">新建</a> <div class="drop mtopad"><a href="product.aspx?rec=add">商品</a> <a href="article.aspx?rec=add">文章</a> <a href="nav.aspx?rec=add">自定义导航</a> <a href="show.aspx">首页幻灯</a> <a href="page.aspx?rec=add">单页面</a> <a href="manager.aspx?rec=add">管理员</a> <a href="link.aspx"></a> </div> </li> <li><a href="../index.aspx" target="_blank">查看站点</a></li> <li><a href="index.aspx?rec=clear_cache">清除缓存</a></li> <li><a href="http://help.douco.com" target="_blank">帮助</a></li> <li class="noright"><a href="module.aspx">Douaspx+</a></li> </ul> <ul class="navRight"> <li class="M noleft"><a href="JavaScript:voID(0);">您好,admin</a> <div class="drop mUser"> <a href="manager.aspx?rec=edit&ID=1">编辑我的个人资料</a> <a href="manager.aspx?rec=cloud_account">设置云账户</a> </div> </li> <li class="noright"><a href="login.aspx?rec=logout">退出</a></li> </ul> </div> </div></div><!-- dchead 结束 --> <div ID="dcleft"><div ID="menu"> <ul class="top"> <li><a href="index.aspx"><i class="home"></i><em>管理首页</em></a></li> </ul> <ul> <li><a href="system.aspx"><i class="system"></i><em>系统设置</em></a></li> <li><a href="nav.aspx"><i class="nav"></i><em>自定义导航栏</em></a></li> <li class="cur"><a href="show.aspx"><i class="show"></i><em>首页幻灯广告</em></a></li> <li><a href="page.aspx"><i class="page"></i><em>单页面管理</em></a></li> </ul> <ul> <li><a href="article_category.aspx"><i class="articleCat"></i><em>文章分类</em></a></li> <li><a href="article.aspx"><i class="article"></i><em>文章列表</em></a></li> </ul> <ul class="bot"> <li><a href="backup.aspx"><i class="backup"></i><em>数据备份</em></a></li> <li><a href="mobile.aspx"><i class="mobile"></i><em>手机版</em></a></li> <li><a href="theme.aspx"><i class="theme"></i><em>设置模板</em></a></li> <li><a href="manager.aspx"><i class="manager"></i><em>网站管理员</em></a></li> <li><a href="manager.aspx?rec=manager_log"><i class="managerLog"></i><em> *** 作记录</em></a></li> </ul></div></div> <div ID="dcMain"> <!-- 当前位置 --><div ID="urHere">Douaspx 管理中心<b>></b><strong>首页幻灯广告</strong> </div> <div class="mainBox imgModule"> <h3>首页幻灯广告</h3> <table wIDth="100%" border="0" cellpadding="8" cellspacing="0" class="tableBasic"> <tr> <th>添加幻灯</th> <th>幻灯列表</th> </tr> <tr> <td wIDth="350" valign="top"> <form action="show.aspx?rec=insert" method="post" enctype="multipart/form-data"> <table wIDth="100%" border="0" cellpadding="8" cellspacing="0" class="tableOnebor"> <tr> <td><b>幻灯名称</b><asp:TextBox ID="SlIDename" size="20" class="inpMain" runat="server"></asp:TextBox> </td> </tr> <tr> <td><b>幻灯图片</b> <div ID="prevIEw"> </div> <div ID="div_pic"><asp:Image ID="pic" runat="server" ImageUrl="~/Images/photo_icon.png" /></div> <asp:fileUpload ID="pic_upload" runat="server" /><asp:Label ID="lbl_pic" runat="server" class="pic_text"></asp:Label> </td> </tr> <tr> <td><b>链接地址</b> <asp:TextBox name="show_link" value="" size="40" class="inpMain" runat="server" ID="SlIDeUrl"></asp:TextBox> </td> </tr> <tr> <td><b>描述</b> <asp:TextBox ID="SlIDeSort" value="简介说明" size="20" class="inpMain" runat="server"></asp:TextBox> </td> </tr> <tr> <td> <input type="hIDden" name="token" value="79db104d" /> <asp:button ID="submit" class="btn" runat="server" Text="提交" onclick="submit_Click"></asp:button> </td> </tr> </table> </form> </td> <td valign="top"> <table wIDth="100%" border="0" cellpadding="8" cellspacing="0" class="tableOnebor"> <tr> <td class="style6">幻灯片</td> <td class="style3">幻灯片名称</td> <td class="style5">跳转的链接</td> <td align="center" class="style2">描述</td> <td wIDth="80" align="center"> *** 作</td> </tr> <%= SlIDeShow()%> </table> </td> </tr> </table> </div> </div> <div class="clear"></div><div ID="dcFooter"> <div ID="footer"> <div class="line"></div> <ul> 版权所有 © 2013-2015 网络科技有限公司,并保留所有权利。 </ul> </div></div><!-- dcFooter 结束 --><div class="clear"></div> </div> </form></body></aspx>总结
以上是内存溢出为你收集整理的一、WebFrom 图片上传全部内容,希望文章能够帮你解决一、WebFrom 图片上传所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)