C#上传图片.docx

上传人:b****5 文档编号:12542068 上传时间:2023-04-20 格式:DOCX 页数:41 大小:22.96KB
下载 相关 举报
C#上传图片.docx_第1页
第1页 / 共41页
C#上传图片.docx_第2页
第2页 / 共41页
C#上传图片.docx_第3页
第3页 / 共41页
C#上传图片.docx_第4页
第4页 / 共41页
C#上传图片.docx_第5页
第5页 / 共41页
点击查看更多>>
下载资源
资源描述

C#上传图片.docx

《C#上传图片.docx》由会员分享,可在线阅读,更多相关《C#上传图片.docx(41页珍藏版)》请在冰豆网上搜索。

C#上传图片.docx

C#上传图片

C#上传图片.txt结婚就像是给自由穿件棉衣,活动起来不方便,但会很温暖。

谈恋爱就像剥洋葱,总有一层让你泪流。

1.最简单的单文件上传(没花头)

效果图:

说明:

这是最基本的文件上传,在asp.Net1.x中没有这个FileUpload控件,只有html的上传控件,那时候要把html控件转化为服务器控件,很不好用。

其实所有文件上传的美丽效果都是从这个FileUpload控件衍生,第一个例子虽然简单却是根本。

后台代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

}

protectedvoidbt_upload_Click(objectsender,EventArgse)

{

try

{

if(FileUpload1.PostedFile.FileName=="")

{

this.lb_info.Text="请选择文件!

";

}

else

{

stringfilepath=FileUpload1.PostedFile.FileName;

stringfilename=filepath.Substring(filepath.LastIndexOf("\\")+1);

stringserverpath=Server.MapPath("images/")+filename;

FileUpload1.PostedFile.SaveAs(serverpath);

this.lb_info.Text="上传成功!

";

}

}

catch(Exceptionex)

{

this.lb_info.Text="上传发生错误!

原因是:

"+ex.ToString();

}

}

}

前台代码:

343px">

100px">

单文件上传

100px">

100px">

FileUploadID="FileUpload1"runat="server"Width="475px"/>

100px">

ButtonID="bt_upload"runat="server"OnClick="bt_upload_Click"Text="上传"/>

100px;height:

21px;">

LabelID="lb_info"runat="server"ForeColor="Red"Width="183px">

Label>

100px;height:

21px">

2.多文件上传

效果图:

 

后台代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

}

protectedvoidbt_upload_Click(objectsender,EventArgse)

{

if((FileUpload1.PostedFile.FileName==""&&FileUpload2.PostedFile.FileName=="")&&FileUpload3.PostedFile.FileName=="")

{

this.lb_info.Text="请选择文件!

";

}

else

{

HttpFileCollectionmyfiles=Request.Files;

for(inti=0;i

{

HttpPostedFilemypost=myfiles[i];

try

{

if(mypost.ContentLength>0)

{

stringfilepath=mypost.FileName;

stringfilename=filepath.Substring(filepath.LastIndexOf("\\")+1);

stringserverpath=Server.MapPath("images/")+filename;

mypost.SaveAs(serverpath);

this.lb_info.Text="上传成功!

";

}

}

catch(Exceptionerror)

{

this.lb_info.Text="上传发生错误!

原因:

"+error.ToString();

}

}

}

}

}

前台代码:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:

//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

//www.w3.org/1999/xhtml">

多文件上传清清月儿http:</p><p>//blog.csdn.Net/21as.Net/

343px">

100px">

多文件上传

100px">

100px">

FileUploadID="FileUpload1"runat="server"Width="475px"/>

100px">

100px">

FileUploadID="FileUpload2"runat="server"Width="475px"/>

100px">

100px">

FileUploadID="FileUpload3"runat="server"Width="475px"/>

100px">

100px">

ButtonID="bt_upload"runat="server"OnClick="bt_upload_Click"Text="一起上传"/>

LabelID="lb_info"runat="server"ForeColor="Red"Width="183px">

Label>

100px">

3.客户端检查上传文件类型(以上传图片为例)

效果图:

后台代码和1.最简单的单文件上传一样;

前台代码:

<%@PageLanguage="C#"AutoEventWireup="true"CodeFile="Default.aspx.cs"Inherits="_Default"%>

DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http:

//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

//www.w3.org/1999/xhtml">

清清月儿.Net/21as.Nethttp:</p><p>//blog.csdn.Net/21as.Net

functionCheck_FileType()

{

varstr=document.getElementById("FileUpload1").value;

varpos=str.lastIndexOf(".");

varlastname=str.substring(pos,str.length)

if(lastname.toLowerCase()!

=".jpg"&&lastname.toLowerCase()!

=".gif")

{

alert("您上传的文件类型为"+lastname+",图片必须为.jpg,.gif类型");

returnfalse;

}

else

{

returntrue;

}

}

343px">

104px">

文件上传判断

100px">

104px">

FileUploadID="FileUpload1"runat="server"Width="400px"/>

100px">

ButtonID="bt_upload"runat="server"OnClick="bt_upload_Click"Text="上传"OnClientClick="returnCheck_FileType()"/>

104px;height:

21px;">

LabelID="lb_info"runat="server"ForeColor="Red"Width="183px">

Label>

100px;height:

21px">

说明:

点击上传时先触发客户端事件Check_FileType;

4.服务器端检查上传文件类型(以上传图片为例)

效果图:

 

后台代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclass_Default:

System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{

}

protectedvoidbt_upload_Click(objectsender,EventArgse)

{

try

{

if(FileUpload1.PostedFile.FileName=="")

{

this.lb_info.Text="请选择文件!

";

}

else

{

stringfilepath=FileUpload1.PostedFile.FileName;

if(IsAllowedExtension(FileUpload1)==true)

{

stringfilename=filepath.Substring(filepath.LastIndexOf("\\")+1);

stringserverpath=Server.MapPath("images/")+filename;

FileUpload1.PostedFile.SaveAs(serverpath);

this.lb_info.Text="上传成功!

";

}

else

{

this.lb_info.Text="请上传图片";

}

}

}

catch(Exceptionerror)

{

this.lb_info.Text="上传发生错误!

原因:

"+error.ToString();

}

}

publicstaticboolIsAllowedExtension(FileUploadhifile)

{

stringstrOldFilePath="",strExtension="";

string[]arrExtension={".gif",".jpg",".jpeg",".bmp",".png"};

if(hifile.PostedFile.FileName!

=string.Empty)

{

strOldFilePath=hifile.PostedFile.FileName;

strExtension=strOldFilePath.Substring(strOldFilePath.LastIndexOf("."));

for(inti=0;i

{

if(strExtension.Equals(arrExtension[i]))

{

returntrue;

}

}

}

returnfalse;

}

}

 

5.服务器端检查上传文件类型(可以检测真正文件名)

其实方法4并不好,因为用户可以把XXX.txt伪装为XXX.jpg。

效果图:

 

后台代码:

usingSystem;

usingSystem.Data;

usingSystem.Configuration;

usingSystem.Collections;

usingSystem.Web;

usingSystem.Web.Security;

usingSystem.Web.UI;

usingSystem.Web.UI.WebControls;

usingSystem.Web.UI.WebControls.WebParts;

usingSystem.Web.UI.HtmlControls;

publicpartialclass_Default:

System.Web.UI.Page

{

//清清月儿http:

//blog.csdn.Net/21as.Net

protectedvoidPage_Load(objectsender,EventArgse)

{

}

protectedvoidbt_upload_Click(objectsender,EventArgse)

{

try

{

if(FileUpload1.PostedFile.FileName=="")

{

this.lb_info.Text="请选择文件!

";

}

else

{

stringfilepath=FileUpload1.PostedFile.FileName;

if(IsAllowedExtension(FileUpload1)==true)

{

stringfilename=filepath.Substring(filepath.LastIndexOf("\\")+1);

stringserverpath=Server.MapPath("images/")+filename;

FileUpload1.PostedFile.SaveAs(serverpath);

this.lb_info.Text="上传成功!

";

}

else

{

this.lb_info.Text="请上传图片";

}

}

}

catch(Exceptionerror)

{

this.lb_info.Text="上传发生错误!

原因:

"+error.ToString();

}

}

publicstaticboolIsAllowedExtension(FileUploadhifile)

{

System.IO.FileStreamfs=newSystem.IO.FileStream(hifile.PostedFile.FileName,System.IO.FileMode.Open,System.IO.FileAccess.Read);

System.IO.BinaryReaderr=newSystem.IO.BinaryReader(fs);

stringfileclass="";

bytebuffer;

try

{

buffer=r.ReadByte();

fileclass=buffer.ToString();

buffer=r.ReadByte();

fileclass+=buffer.ToString();

}

catch

{

}

r.Close();

fs.Close();

if(fileclass=="255216"||fileclass=="7173")//说明255216是jpg;7173是gif;6677是BMP,13780是PNG;7790是exe,8297是rar

{

returntrue;

}

else

{

returnfalse;

}

}

}

6.上传文件文件名唯一性处理(时间戳+SessionID)

效果图:

说明:

年月日时分秒+临时session+原文件名如果大家怕还会重复可以加GUID

后台代码:

 

try

{

if(FileUpload1.PostedFile.FileName=="")

{

this.lb_info.Text="请选择文件!

";

}

else

{

stringfilepath=FileUpload1.PostedFile.FileName;

stringfilename=filepath.Substring(filepath.LastIndexOf("\\")+1);

stringserverpath=Server.MapPath("images/")+System.DateTime.Now.ToString("yyy-MM-dd-hh-mm-ss")+Session.SessionID+filename;

FileUpload1.PostedFile.SaveAs(serverpath);

this.lb_info.Text="上传成功!

";

}

}

catch(Exceptionerror)

{

this.lb_info.Text="上传发生错误!

原因:

"+error.ToString();

}

注:

GUID的方法:

GuidmyGuid=Guid.NewGuid();

7.上传图片生成等比例缩略图

效果图:

 

缩略图代码:

ImageThumbnail.cs

usingSystem;

usingSystem.IO;

usingSystem.Drawing;

usingSystem.Drawing.Imaging;

publicclassImageThumbnail

{

publicImageResourceImage;

privateint

展开阅读全文
相关资源
猜你喜欢
相关搜索

当前位置:首页 > 工程科技 > 电力水利

copyright@ 2008-2022 冰豆网网站版权所有

经营许可证编号:鄂ICP备2022015515号-1