C#上传图片.docx
- 文档编号:12542068
- 上传时间:2023-04-20
- 格式:DOCX
- 页数:41
- 大小:22.96KB
C#上传图片.docx
《C#上传图片.docx》由会员分享,可在线阅读,更多相关《C#上传图片.docx(41页珍藏版)》请在冰豆网上搜索。
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"> //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">