Asp.net FileUpload+Image制作頭像效果示例代碼
FileUpLoad控件使用介紹
FileUpLoad控件的PostedFile屬性主要獲取上傳文件的一些基礎(chǔ)信息。
.ContentLength
獲取上傳文件的大小。返回值為int類型,單位為字節(jié)。
用途
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上傳的默認(rèn)值
ASP.NET的默認(rèn)最大上傳值是4M,通過(guò)修改web.config,可以實(shí)現(xiàn)上傳大文件:
<system.web>
<httpRuntime maxRequestLength="4096"http://這里的4096就是4m,原先是默認(rèn)的。你改一下這個(gè),就可以了。
executionTimeout="3600"
/>
</system.web>
.ContentType
獲取上傳文件的MIME內(nèi)容類型。PostedFile.ContentType這個(gè)屬性通常用來(lái)判斷上傳文件的MIME類型,我們通常以此來(lái)拒絕某些類型的上傳,比如僅允許上傳圖像文件,或者指定固定圖片的格式。
在net,做上傳文件判斷文件格式的寫(xiě)法總結(jié):
".*"="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
獲取上傳文件的完整路徑及名稱。此路徑為改文件所在在客戶端文件中的路徑。通常用這個(gè)屬性來(lái)獲取上傳文件的名稱并保保存。
.FileUpload1.SaveAs
將上傳文件保存到指定路徑的文件下。通常借助Server.MapPath()方法保存文件到服務(wù)器指定文件下。(獲取Web服務(wù)器指定虛擬路徑相對(duì)應(yīng)的物理文件路徑)
圖片上傳實(shí)例
FileUpload+Image控件實(shí)現(xiàn)上傳指定圖片大小、格式、保存到指定路徑文件下。
.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">獲取圖片大小,以字節(jié)為單位</SPAN>
if(length<4000)
{
Response.Write("<script language='javascript'>alert('您選擇的圖片過(guò)大!');</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">獲取文件擴(kuò)展名</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">將圖片以當(dāng)前時(shí)間命名保存,避免上傳圖片命名重復(fù)</SPAN>
<SPAN style="COLOR: #ff0000">//設(shè)置上傳圖片大小為原圖片大小</SPAN>
//System.Drawing.Image img = System.Drawing.Image.FromFile(this.Server.MapPath("Photo") + "\\" + name + "." + extensions);//獲得圖片文件
//int width = img.Width;
//this.Image1.Width = width;//這是寬設(shè)置Image控件的寬度為圖片寬度
//int height = img.Height;
//this.Image1.Height = height;//這是高,設(shè)置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("文件刪除失?。?);
}
}
}
}
}
效果
文件上傳前
文件上傳后
- asp.net顯示圖片到指定的Image控件中 具體實(shí)現(xiàn)
- ASP.NET中CheckBoxList復(fù)選框列表控件詳細(xì)使用方法
- ASP.NET中DropDownList下拉框列表控件綁定數(shù)據(jù)的4種方法
- ASP.NET中FileUpload文件上傳控件應(yīng)用實(shí)例
- ASP.NET中HyperLink超鏈接控件的使用方法
- ASP.NET中Image控件使用詳解
- ASP.NET中ImageButton圖片按鈕控件的使用
- ASP.NET 中 Button、LinkButton和ImageButton 三種控件的使用詳解
- ASP.NET中訪問(wèn)DataGrid中所有控件值的方法
- ASP.NET 中ImageMap控件的用法
相關(guān)文章
Entity?Framework生成DataBase?First模式
本文詳細(xì)講解了Entity?Framework生成DataBase?First模式的方法,文中通過(guò)示例代碼介紹的非常詳細(xì)。對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-03-03
ASP.NET Core 過(guò)濾器中使用依賴注入知識(shí)點(diǎn)總結(jié)
在本篇文章里小編給大家分享的是關(guān)于ASP.NET Core 過(guò)濾器中使用依賴注入的相關(guān)知識(shí)點(diǎn)內(nèi)容,需要的朋友們學(xué)習(xí)下。2019-10-10
Forms身份認(rèn)證在IE11下無(wú)法保存Cookie的問(wèn)題
這篇文章主要介紹了Forms身份認(rèn)證在IE11下無(wú)法保存Cookie問(wèn)題的解決方法,需要的朋友可以參考下2014-05-05
C# 中使用iTextSharp組件創(chuàng)建PDF的簡(jiǎn)單方法
C# 中使用iTextSharp組件創(chuàng)建PDF的簡(jiǎn)單方法,需要的朋友可以參考一下2013-03-03
ASP.NET Core文件壓縮常見(jiàn)使用誤區(qū)(最佳實(shí)踐)
本文給大家分享ASP.NET Core文件壓縮常見(jiàn)的三種誤區(qū),就每種誤區(qū)給大家講解的非常詳細(xì),是項(xiàng)目實(shí)踐的最佳紀(jì)錄,對(duì)ASP.NET Core文件壓縮相關(guān)知識(shí)感興趣的朋友一起看看吧2021-05-05
ASP.NET實(shí)現(xiàn)上傳圖片并生成縮略圖的方法
這篇文章主要介紹了ASP.NET實(shí)現(xiàn)上傳圖片并生成縮略圖的方法,提供了一個(gè)asp.net上傳圖片與生成縮略圖的方法,并給出了改進(jìn)程序供大家對(duì)比分析,需要的朋友可以參考下2015-12-12
asp.net?web?api2設(shè)置默認(rèn)啟動(dòng)登錄頁(yè)面的方法
這篇文章主要介紹了asp.net?web?api2設(shè)置默認(rèn)啟動(dòng)登錄頁(yè)面的方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-09-09
深入學(xué)習(xí).net驗(yàn)證碼生成及使用方法
這篇文章主要介紹了.net驗(yàn)證碼生成及使用方法,先了解驗(yàn)證碼是什么以及其作用,最后分享了如何制作驗(yàn)證碼,內(nèi)容很全面,感興趣的小伙伴們可以參考一下2015-11-11

