无为清净楼资源网 Design By www.qnjia.com
本文为大家分享了fileupload控件实现上传图片后并进行预览图片的功能,并对web.config进行了配置,先看一下最终效果:
页面代码:
<form id="form1" runat="server"> <div> <asp:FileUpload ID="FileUpload1" runat="server" /> <asp:Button ID="Button1" runat="server" Text="上传" Width="54px" OnClick="Button1_Click" /> <asp:Label ID="Label1" runat="server" Text="" Style="color: Red"></asp:Label> <asp:Image runat="server" ID="Image1" Style="z-index: 102; left: 20px; position: absolute; top: 49px" Width="73px" /> </div> </form>
后台代码:
using System; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; namespace Web.File { public partial class WebForm1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { } #region 文件上传 /// <summary> /// 文件上传 /// </summary> protected void Button1_Click(object sender, EventArgs e) { if (FileUpload1.FileName == "") { this.Label1.Text = "上传文件不能为空"; return; } bool fileIsValid = false; //如果确认了上传文件,则判断文件类型是否符合要求 if (this.FileUpload1.HasFile) { //获取上传文件的后缀 String fileExtension = System.IO.Path.GetExtension(this.FileUpload1.FileName).ToLower(); String[] restrictExtension = { ".gif", ".jpg", ".bmp", ".png" }; //判断文件类型是否符合要求 for (int i = 0; i < restrictExtension.Length; i++) { if (fileExtension == restrictExtension[i]) { fileIsValid = true; } //如果文件类型符合要求,调用SaveAs方法实现上传,并显示相关信息 if (fileIsValid == true) { //上传文件是否大于10M if (FileUpload1.PostedFile.ContentLength > (10 * 1024 * 1024)) { this.Label1.Text = "上传文件过大"; return; } try { this.Image1.ImageUrl = "~/File/" + FileUpload1.FileName; this.FileUpload1.SaveAs(Server.MapPath("~/File/") + FileUpload1.FileName); this.Label1.Text = "文件上传成功!"; } catch { this.Label1.Text = "文件上传失败!"; } finally { } } else { this.Label1.Text = "只能够上传后缀为.gif,.jpg,.bmp,.png的文件"; } } } } #endregion } }
Web.config 配置:
<!--因为FileUpload 控件上传最大为4M,如果要上传更大文件,改下maxRequestLength的大小--> <configuration> <system.web> <compilation debug="true" targetFramework="4.0" /> <httpRuntime requestValidationMode="2.0" maxRequestLength="10485760" executionTimeout="3600" appRequestQueueLimit="10000"/> </system.web> </configuration>
为大家附3个精彩的专题:
ASP.NET控件使用手册
ASP.NET数据绑定控件使用汇总
ASP.NET控件使用汇总
亲,你可以在自己的项目中实现fileupload控件上传图片并进行预览图片的功能,这样网站更具有实用性,基本步骤就是这些,可能还有小编遗漏的地方,希望大家谅解。
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月27日
2024年09月27日
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 金佩姗.1997-喜新恋旧【现代派】【WAV+CUE】
- 梁静茹.2007-崇拜【相信音乐】【WAV+CUE】
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《云水禅心 - 佛曲古筝礼赞1-2》1CD[MP3][190MB]
- 纯音入心系列纯音乐《精心传神-古筝》1CD[MP3][363.1MB]
- 纯音入心系列纯音乐《琴瑟古风之古筝精选曲》1CD[MP3][354.1MB]
- 陈晓东.1997-在乎你感受(EP)【宝丽金】【WAV+CUE】
- 黄凯芹.1990-给你留念(2024环球MQA-UHQCD限量版)【环球】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】
- 纯音入心系列纯音乐《古筝佛曲》1CD[MP3][411.1MB]
- 纯音入心系列纯音乐《古筝世界名曲》1CD[MP3][38.9MB]
- 纯音入心系列纯音乐《古筝新奏-菩提妙音》1CD[MP3][112.3MB]
- 陈思安.1993-怀念福建金曲5CD【歌丽美】【WAV+CUE】
- 张学友.1999-有个人【宝丽金】【WAV+CUE】