当前位置: 移动技术网 > IT编程>开发语言>.net > Asp.net FileUpload+Image制作头像效果示例代码

Asp.net FileUpload+Image制作头像效果示例代码

2017年12月12日  | 移动技术网IT编程  | 我要评论

血狐杀戮,司机车钥匙竟是刀,婚姻八字配对

在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("文件删除失败!");
}
}
}
}
}

效果
文件上传前
 
文件上传后

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网