Asp.net FileUpload+Image制作头像效果示例代码
更新时间:2021年9月22日 10:14 点击:1521
在Web开发中会经常使用到个人信息注册,而个人信息中通常需要自己的头像或者照片。今天主要介绍一下使用FileUpload+img控件上传照片。
FileUpLoad控件使用介绍
FileUpLoad控件的PostedFile属性主要获取上传文件的一些基础信息。
.ContentLength
获取上传文件的大小。返回值为int类型,单位为字节。
用途
1、判断上传文件的大小
if (this.FileUpload1.PostedFile.ContentLength <= 4000000) //4M
{
Response.Write("<script>alert('" +FileUpload1.PostedFile.ContentLength + "')</script>");
}
else
{
Response.Write("<script>alert('太大了不能上传!')</script>");
}
其中FileUpload1是上传控件的ID,this.FileUpload1.PostedFile.ContentLength是上传文件的大小
2、修改ASP.NET上传的默认值
ASP.NET的默认最大上传值是4M,通过修改web.config,可以实现上传大文件:
<system.web>
<httpRuntime maxRequestLength="4096"//这里的4096就是4m,原先是默认的。你改一下这个,就可以了。
executionTimeout="3600"
/>
</system.web>
.ContentType
获取上传文件的MIME内容类型。PostedFile.ContentType这个属性通常用来判断上传文件的MIME类型,我们通常以此来拒绝某些类型的上传,比如仅允许上传图像文件,或者指定固定图片的格式。
在net,做上传文件判断文件格式的写法总结:
".*"="application/octet-stream"
".001"="application/x-001"
".301"="application/x-301"
".323"="text/h323"
".906"="application/x-906"
".907"="drawing/907"
".a11"="application/x-a11"
".acp"="audio/x-mei-aac"
".ai"="application/postscript"
".aif"="audio/aiff"
".aifc"="audio/aiff"
".aiff"="audio/aiff"
".anv"="application/x-anv"
".asa"="text/asa"
".asf"="video/x-ms-asf"
".asp"="text/asp"
".asx"="video/x-ms-asf"
".au"="audio/basic"
".avi"="video/avi"
".awf"="application/vnd.adobe.workflow"
".biz"="text/xml"
".bmp"="application/x-bmp"
".bot"="application/x-bot"
".c4t"="application/x-c4t"
".c90"="application/x-c90"
".cal"="application/x-cals"
".cat"="application/vnd.ms-pki.seccat"
".cdf"="application/x-netcdf"
".cdr"="application/x-cdr"
".cel"="application/x-cel"
".cer"="application/x-x509-ca-cert"
".cg4"="application/x-g4"
".cgm"="application/x-cgm"
".cit"="application/x-cit"
".class"="java/*"
".cml"="text/xml"
".cmp"="application/x-cmp"
".cmx"="application/x-cmx"
".cot"="application/x-cot"
".crl"="application/pkix-crl"
".crt"="application/x-x509-ca-cert"
".csi"="application/x-csi"
".css"="text/css"
".cut"="application/x-cut"
".dbf"="application/x-dbf"
".dbm"="application/x-dbm"
".dbx"="application/x-dbx"
".dcd"="text/xml"
".dcx"="application/x-dcx"
".der"="application/x-x509-ca-cert"
".dgn"="application/x-dgn"
".dib"="application/x-dib"
".dll"="application/x-msdownload"
".doc"="application/msword"
".dot"="application/msword"
".drw"="application/x-drw"
".dtd"="text/xml"
".dwf"="Model/vnd.dwf"
".dwf"="application/x-dwf"
".dwg"="application/x-dwg"
".dxb"="application/x-dxb"
".dxf"="application/x-dxf"
".edn"="application/vnd.adobe.edn"
".emf"="application/x-emf"
".eml"="message/rfc822"
".ent"="text/xml"
".epi"="application/x-epi"
".eps"="application/x-ps"
".eps"="application/postscript"
".etd"="application/x-ebx"
".exe"="application/x-msdownload"
".fax"="image/fax"
".fdf"="application/vnd.fdf"
".fif"="application/fractals"
".fo"="text/xml"
".frm"="application/x-frm"
".g4"="application/x-g4"
".gbr"="application/x-gbr"
".gcd"="application/x-gcd"
".gif"="image/gif"
".gl2"="application/x-gl2"
".gp4"="application/x-gp4"
".hgl"="application/x-hgl"
".hmr"="application/x-hmr"
".hpg"="application/x-hpgl"
".hpl"="application/x-hpl"
".hqx"="application/mac-binhex40"
".hrf"="application/x-hrf"
".hta"="application/hta"
".htc"="text/x-component"
".htm"="text/html"
".html"="text/html"
".htt"="text/webviewhtml"
".htx"="text/html"
".icb"="application/x-icb"
".ico"="image/x-icon"
".ico"="application/x-ico"
".iff"="application/x-iff"
".ig4"="application/x-g4"
".igs"="application/x-igs"
".iii"="application/x-iphone"
".img"="application/x-img"
".ins"="application/x-internet-signup"
".isp"="application/x-internet-signup"
".IVF"="video/x-ivf"
".java"="java/*"
".jfif"="image/jpeg"
".jpe"="image/jpeg"
".jpe"="application/x-jpe"
".jpeg"="image/jpeg"
".jpg"="application/x-jpg"
".js"="application/x-javascript"
".jsp"="text/html"
".la1"="audio/x-liquid-file"
".lar"="application/x-laplayer-reg"
".latex"="application/x-latex"
".lavs"="audio/x-liquid-secure"
".lbm"="application/x-lbm"
".lmsff"="audio/x-la-lms"
".ls"="application/x-javascript"
".ltr"="application/x-ltr"
.FileName
获取上传文件的完整路径及名称。此路径为改文件所在在客户端文件中的路径。通常用这个属性来获取上传文件的名称并保保存。
.FileUpload1.SaveAs
将上传文件保存到指定路径的文件下。通常借助Server.MapPath()方法保存文件到服务器指定文件下。(获取Web服务器指定虚拟路径相对应的物理文件路径)
图片上传实例
FileUpload+Image控件实现上传指定图片大小、格式、保存到指定路径文件下。
.axps
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Fileup.aspx.cs" Inherits="Testcookie.Fileup" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" Height="180px" Width="191px" ImageUrl="~/Photo/untitled.png" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnupload" runat="server" Text="Button" onclick="btnupload_Click<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">" /></SPAN>
<asp:Button ID="btndelete" runat="server" Text="Button" onclick="btndelete_Click" />
</div>
</form>
</body>
</html>
.axps.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Testcookie
{
public partial class Fileup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnupload_Click(object sender, EventArgs e)
{
int length= this.FileUpload1.PostedFile.ContentLength;//<SPAN style="COLOR: #ff0000">获取图片大小,以字节为单位</SPAN>
if(length<4000)
{
Response.Write("<script language='javascript'>alert('您选择的图片过大!');</script>");
}
else{
String type=this.FileUpload1.PostedFile.ContentType;//<SPAN style="COLOR: #ff0000">获取上传文件类型</SPAN>
String fullfilename = this.FileUpload1.PostedFile.FileName;//<SPAN style="COLOR: #ff0000">获取上传文件在客户端的路径及名称</SPAN>
String filename = fullfilename.Substring(fullfilename.LastIndexOf("\\")+1);
String extensions = filename.Substring(filename.LastIndexOf (".")+1);//<SPAN style="COLOR: #ff0000">获取文件扩展名</SPAN>
string name=DateTime.Now.ToString("yyyyMMddHHmmss");
if (type == "image/jpeg" || type == "image/png" )//限制上传格式
{
this.FileUpload1.SaveAs(Server.MapPath("Photo") + "\\" + name + "." + extensions);//<SPAN style="COLOR: #ff0000">将图片以当前时间命名保存,避免上传图片命名重复</SPAN>
<SPAN style="COLOR: #ff0000">//设置上传图片大小为原图片大小</SPAN>
//System.Drawing.Image img = System.Drawing.Image.FromFile(this.Server.MapPath("Photo") + "\\" + name + "." + extensions);//获得图片文件
//int width = img.Width;
//this.Image1.Width = width;//这是宽设置Image控件的宽度为图片宽度
//int height = img.Height;
//this.Image1.Height = height;//这是高,设置Image控件的高度为图片的高度
this.Image1.ImageUrl = "Photo/" + name + "." + extensions;
}
else
{
Response.Write("<script language='javascript'>alert('您选择的图片有误!');</script>");
}
}
}
protected void btndelete_Click (object sender, EventArgs e)
{
<SPAN style="COLOR: #ff0000">//删除图片</SPAN>
if (System.IO.File.Exists(Server.MapPath("Photo")+"\\20130808084433.PNG"))
{
try
{
System.IO.File.Delete(Server.MapPath("Photo") + "\\20130808084433.PNG");
}
catch
{
throw new Exception("文件删除失败!");
}
}
}
}
}
效果
文件上传前
文件上传后
FileUpLoad控件使用介绍
FileUpLoad控件的PostedFile属性主要获取上传文件的一些基础信息。
.ContentLength
获取上传文件的大小。返回值为int类型,单位为字节。
用途
1、判断上传文件的大小
if (this.FileUpload1.PostedFile.ContentLength <= 4000000) //4M
{
Response.Write("<script>alert('" +FileUpload1.PostedFile.ContentLength + "')</script>");
}
else
{
Response.Write("<script>alert('太大了不能上传!')</script>");
}
其中FileUpload1是上传控件的ID,this.FileUpload1.PostedFile.ContentLength是上传文件的大小
2、修改ASP.NET上传的默认值
ASP.NET的默认最大上传值是4M,通过修改web.config,可以实现上传大文件:
复制代码 代码如下:
<system.web>
<httpRuntime maxRequestLength="4096"//这里的4096就是4m,原先是默认的。你改一下这个,就可以了。
executionTimeout="3600"
/>
</system.web>
.ContentType
获取上传文件的MIME内容类型。PostedFile.ContentType这个属性通常用来判断上传文件的MIME类型,我们通常以此来拒绝某些类型的上传,比如仅允许上传图像文件,或者指定固定图片的格式。
在net,做上传文件判断文件格式的写法总结:
复制代码 代码如下:
".*"="application/octet-stream"
".001"="application/x-001"
".301"="application/x-301"
".323"="text/h323"
".906"="application/x-906"
".907"="drawing/907"
".a11"="application/x-a11"
".acp"="audio/x-mei-aac"
".ai"="application/postscript"
".aif"="audio/aiff"
".aifc"="audio/aiff"
".aiff"="audio/aiff"
".anv"="application/x-anv"
".asa"="text/asa"
".asf"="video/x-ms-asf"
".asp"="text/asp"
".asx"="video/x-ms-asf"
".au"="audio/basic"
".avi"="video/avi"
".awf"="application/vnd.adobe.workflow"
".biz"="text/xml"
".bmp"="application/x-bmp"
".bot"="application/x-bot"
".c4t"="application/x-c4t"
".c90"="application/x-c90"
".cal"="application/x-cals"
".cat"="application/vnd.ms-pki.seccat"
".cdf"="application/x-netcdf"
".cdr"="application/x-cdr"
".cel"="application/x-cel"
".cer"="application/x-x509-ca-cert"
".cg4"="application/x-g4"
".cgm"="application/x-cgm"
".cit"="application/x-cit"
".class"="java/*"
".cml"="text/xml"
".cmp"="application/x-cmp"
".cmx"="application/x-cmx"
".cot"="application/x-cot"
".crl"="application/pkix-crl"
".crt"="application/x-x509-ca-cert"
".csi"="application/x-csi"
".css"="text/css"
".cut"="application/x-cut"
".dbf"="application/x-dbf"
".dbm"="application/x-dbm"
".dbx"="application/x-dbx"
".dcd"="text/xml"
".dcx"="application/x-dcx"
".der"="application/x-x509-ca-cert"
".dgn"="application/x-dgn"
".dib"="application/x-dib"
".dll"="application/x-msdownload"
".doc"="application/msword"
".dot"="application/msword"
".drw"="application/x-drw"
".dtd"="text/xml"
".dwf"="Model/vnd.dwf"
".dwf"="application/x-dwf"
".dwg"="application/x-dwg"
".dxb"="application/x-dxb"
".dxf"="application/x-dxf"
".edn"="application/vnd.adobe.edn"
".emf"="application/x-emf"
".eml"="message/rfc822"
".ent"="text/xml"
".epi"="application/x-epi"
".eps"="application/x-ps"
".eps"="application/postscript"
".etd"="application/x-ebx"
".exe"="application/x-msdownload"
".fax"="image/fax"
".fdf"="application/vnd.fdf"
".fif"="application/fractals"
".fo"="text/xml"
".frm"="application/x-frm"
".g4"="application/x-g4"
".gbr"="application/x-gbr"
".gcd"="application/x-gcd"
".gif"="image/gif"
".gl2"="application/x-gl2"
".gp4"="application/x-gp4"
".hgl"="application/x-hgl"
".hmr"="application/x-hmr"
".hpg"="application/x-hpgl"
".hpl"="application/x-hpl"
".hqx"="application/mac-binhex40"
".hrf"="application/x-hrf"
".hta"="application/hta"
".htc"="text/x-component"
".htm"="text/html"
".html"="text/html"
".htt"="text/webviewhtml"
".htx"="text/html"
".icb"="application/x-icb"
".ico"="image/x-icon"
".ico"="application/x-ico"
".iff"="application/x-iff"
".ig4"="application/x-g4"
".igs"="application/x-igs"
".iii"="application/x-iphone"
".img"="application/x-img"
".ins"="application/x-internet-signup"
".isp"="application/x-internet-signup"
".IVF"="video/x-ivf"
".java"="java/*"
".jfif"="image/jpeg"
".jpe"="image/jpeg"
".jpe"="application/x-jpe"
".jpeg"="image/jpeg"
".jpg"="application/x-jpg"
".js"="application/x-javascript"
".jsp"="text/html"
".la1"="audio/x-liquid-file"
".lar"="application/x-laplayer-reg"
".latex"="application/x-latex"
".lavs"="audio/x-liquid-secure"
".lbm"="application/x-lbm"
".lmsff"="audio/x-la-lms"
".ls"="application/x-javascript"
".ltr"="application/x-ltr"
.FileName
获取上传文件的完整路径及名称。此路径为改文件所在在客户端文件中的路径。通常用这个属性来获取上传文件的名称并保保存。
.FileUpload1.SaveAs
将上传文件保存到指定路径的文件下。通常借助Server.MapPath()方法保存文件到服务器指定文件下。(获取Web服务器指定虚拟路径相对应的物理文件路径)
图片上传实例
FileUpload+Image控件实现上传指定图片大小、格式、保存到指定路径文件下。
.axps
复制代码 代码如下:
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Fileup.aspx.cs" Inherits="Testcookie.Fileup" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Image ID="Image1" runat="server" Height="180px" Width="191px" ImageUrl="~/Photo/untitled.png" />
<asp:FileUpload ID="FileUpload1" runat="server" />
<asp:Button ID="btnupload" runat="server" Text="Button" onclick="btnupload_Click<SPAN style="FONT-FAMILY: Arial, Helvetica, sans-serif">" /></SPAN>
<asp:Button ID="btndelete" runat="server" Text="Button" onclick="btndelete_Click" />
</div>
</form>
</body>
</html>
.axps.cs
复制代码 代码如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Testcookie
{
public partial class Fileup : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnupload_Click(object sender, EventArgs e)
{
int length= this.FileUpload1.PostedFile.ContentLength;//<SPAN style="COLOR: #ff0000">获取图片大小,以字节为单位</SPAN>
if(length<4000)
{
Response.Write("<script language='javascript'>alert('您选择的图片过大!');</script>");
}
else{
String type=this.FileUpload1.PostedFile.ContentType;//<SPAN style="COLOR: #ff0000">获取上传文件类型</SPAN>
String fullfilename = this.FileUpload1.PostedFile.FileName;//<SPAN style="COLOR: #ff0000">获取上传文件在客户端的路径及名称</SPAN>
String filename = fullfilename.Substring(fullfilename.LastIndexOf("\\")+1);
String extensions = filename.Substring(filename.LastIndexOf (".")+1);//<SPAN style="COLOR: #ff0000">获取文件扩展名</SPAN>
string name=DateTime.Now.ToString("yyyyMMddHHmmss");
if (type == "image/jpeg" || type == "image/png" )//限制上传格式
{
this.FileUpload1.SaveAs(Server.MapPath("Photo") + "\\" + name + "." + extensions);//<SPAN style="COLOR: #ff0000">将图片以当前时间命名保存,避免上传图片命名重复</SPAN>
<SPAN style="COLOR: #ff0000">//设置上传图片大小为原图片大小</SPAN>
//System.Drawing.Image img = System.Drawing.Image.FromFile(this.Server.MapPath("Photo") + "\\" + name + "." + extensions);//获得图片文件
//int width = img.Width;
//this.Image1.Width = width;//这是宽设置Image控件的宽度为图片宽度
//int height = img.Height;
//this.Image1.Height = height;//这是高,设置Image控件的高度为图片的高度
this.Image1.ImageUrl = "Photo/" + name + "." + extensions;
}
else
{
Response.Write("<script language='javascript'>alert('您选择的图片有误!');</script>");
}
}
}
protected void btndelete_Click (object sender, EventArgs e)
{
<SPAN style="COLOR: #ff0000">//删除图片</SPAN>
if (System.IO.File.Exists(Server.MapPath("Photo")+"\\20130808084433.PNG"))
{
try
{
System.IO.File.Delete(Server.MapPath("Photo") + "\\20130808084433.PNG");
}
catch
{
throw new Exception("文件删除失败!");
}
}
}
}
}
效果
文件上传前
文件上传后
下一篇: .net 读取非标准配置文件的小例子
相关文章
- 这篇文章主要为大家详细介绍了ASP.NET购物车的实现过程,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下...2021-09-22
- 在开发过程中,使用Visual Studio的断点调试功能可以很方便帮我们调试发现程序存在的错误,同样Visual Studio也支持对SQL Server里面的存储过程进行调试,下面就让我们看看具体的调试方法。...2021-09-22
ASP.NET Core根据环境变量支持多个 appsettings.json配置文件
这篇文章主要介绍了ASP.NET Core根据环境变量支持多个 appsettings.json配置文件,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22- 这篇文章主要介绍了记一次EFCore类型转换错误及解决方案,帮助大家更好的理解和学习使用asp.net core,感兴趣的朋友可以了解下...2021-09-22
详解ASP.NET Core 中基于工厂的中间件激活的实现方法
这篇文章主要介绍了ASP.NET Core 中基于工厂的中间件激活的实现方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-09-22asp.net通过消息队列处理高并发请求(以抢小米手机为例)
这篇文章主要介绍了asp.net通过消息队列处理高并发请求(以抢小米手机为例),文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22ASP.NET单选按钮控件RadioButton常用属性和方法介绍
RadioButton又称单选按钮,其在工具箱中的图标为 ,单选按钮通常成组出现,用于提供两个或多个互斥选项,即在一组单选钮中只能选择一个...2021-09-22ASP.NET 2.0中的数据操作:使用两个DropDownList过滤的主/从报表
在前面的指南中我们研究了如何显示一个简单的主/从报表, 该报表使用DropDownList和GridView控件, DropDownList填充类别,GridView显示选定类别的产品. 这类报表用于显示具有...2016-05-19ASP.NET中iframe框架点击左边页面链接 右边显示链接页面内容
这篇文章主要介绍了ASP.NET中iframe框架点击左边页面链接,右边显示链接页面内容的实现代码,感兴趣的小伙伴们可以参考一下...2021-09-22- ASP.NET Web API具有与ASP.NET MVC类似的编程方式,ASP.NET Web API不仅仅具有一个完全独立的消息处理管道,而且这个管道比为ASP.NET MVC设计的管道更为复杂,功能也更为强大。下面创建一个简单的Web API项目,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了ASP.NET连接MySql数据库的2个方法及示例,使用的是MySQL官方组件和ODBC.NET,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了Asp.Net使用Bulk实现批量插入数据的方法,对于进行asp.net数据库程序设计非常有借鉴价值,需要的朋友可以参考下...2021-09-22
在ASP.NET 2.0中操作数据之二十九:用DataList和Repeater来显示数据
本文主要讲解ASP.NET 2.0中如何使用DataList 和 Repeater 来呈现数据,DataList包含一个table标记,而Repeater不会添加任何额外的代码,个人在实际开发中更推荐使用Repeater。...2021-09-22- 这篇文章主要介绍了获取DataTable选择第一行某一列值,需要的朋友可以参考下...2021-09-22
- 这篇文章主要介绍了ASP.Net中的async+await异步编程的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-09-22
- 这篇文章介绍了Asp.net动态生成html页面的方法,有需要的朋友可以参考一下...2021-09-22
- 这篇文章主要介绍了详解ASP.NET Core Token认证,小编觉得挺不错的,现在分享给大家,也给大家做个参考。...2021-09-22
ASP.NET百度Ueditor编辑器实现上传图片添加水印效果
这篇文章主要给大家介绍了ASP.NET百度Ueditor编辑器1.4.3这个版本实现上传图片添加水印效果的相关资料,文中通过图文及示例代码介绍的非常详细,相信对大家具有一定的参考价值,需要的朋友们下面来一起看看吧。...2021-09-22- .net core是最近讨论频率很高的话题,下面这篇文章主要给大家介绍了关于利用.NET Core如何获取操作系统中各种信息的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧...2021-09-22
- 这篇文章主要介绍了asp.net core MVC之实现基于token的认证,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-05-07