您的位置:首页 > 编程语言 > ASP

ASP.NET同时上传多个图片

2014-05-13 22:21 232 查看
http://www.cnblogs.com/superfang/archive/2009/01/09/1233814.html

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;

publicpartialclassimg_upfill:System.Web.UI.Page

{

protectedvoidPage_Load(objectsender,EventArgse)

{


}

protectedvoidButton1_Click(objectsender,EventArgse)

{

//案例信息

stringal_name=TextBox1.Text;//需要写入数据库的可以不管

stringal_link=TextBox2.Text;;//需要写入数据库的可以不管

stringal_sx=TextBox3.Text;;//需要写入数据库的可以不管

intial_sx=int.Parse(al_sx);;//需要写入数据库的可以不管

stringal_class=DropDownList1.SelectedValue;;//需要写入数据库的可以不管

intial_class=int.Parse(al_class);;//需要写入数据库的可以不管

//logo图片格式检验1

stringname1=FileUpload1.FileName;//设置变量name1等于上传文件的全路径名称也就是浏览后的内容

intsize1=FileUpload1.PostedFile.ContentLength;//获取上传文件的大小

stringtype1=FileUpload1.PostedFile.ContentType;//获取上传文件的MIME类型用语非法格式上传判断

stringtype2=name1.Substring(name1.LastIndexOf(".")+1);//截取出上传文件的类型名称如gifjpg重新命名时使用


if(type1=="image/pjpeg"&&size1<112640&&name1!=""||type1=="image/gif"&&size1<112640&&name1!="")//判断文件的类型和大小是否为空如果为JPG或GIF并且不大110KB就算是合法

{

stringnewName=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+"."+type2;//设置一个变量为上传时间的年月日时秒..做为上传文件的新名称

stringimgpath=Server.MapPath("upfill")+"\\"+newName;//Server获取服务器全路径(包括新文件名)

stringsjpath="upfill\\"+newName;//写如数据库的可以不管

FileUpload1.SaveAs(imgpath);//上传文件

Image1.ImageUrl=sjpath;//图片显示

Label1.Text="上传成功";//成功提示

}

else

{

if(name1==""){Label1.Text="请选择上传图片";}

else

{Label1.Text="格式不正确或过大";}//假如没有上传或上传的不符合标准在执行


}


//缩略图格式检验2

stringname2=FileUpload2.FileName;

intsize2=FileUpload2.PostedFile.ContentLength;

strings1type1=FileUpload2.PostedFile.ContentType;

strings2type2=name2.Substring(name2.LastIndexOf(".")+1);


if(s1type1=="image/pjpeg"&&size2<112640&&name2!=""||s1type1=="image/gif"&&size2<112640&&name2!="")

{

stringnewName2=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+"a"+"."+s2type2;

stringimgpath2=Server.MapPath("upfill")+"\\"+newName2;

stringsjpath2="upfill\\"+newName2;

FileUpload2.SaveAs(imgpath2);

Image2.ImageUrl=sjpath2;

Label2.Text="上传成功";

}

else

{

if(name2==""){Label2.Text="请选择上传图片";}

else

{Label2.Text="格式不正确或过大";}

}

//缩略图格式检验3

stringname3=FileUpload3.FileName;

intsize3=FileUpload3.PostedFile.ContentLength;

strings1type3=FileUpload3.PostedFile.ContentType;

strings2type3=name3.Substring(name3.LastIndexOf(".")+1);


if(s1type3=="image/pjpeg"&&size3<112640&&name3!=""||s1type3=="image/gif"&&size3<112640&&name3!="")

{

stringnewName3=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+"b"+"."+s2type3;

stringimgpath3=Server.MapPath("upfill")+"\\"+newName3;

stringsjpath3="upfill\\"+newName3;

FileUpload3.SaveAs(imgpath3);

Image3.ImageUrl=sjpath3;

Label3.Text="上传成功";

}

else

{

if(name3==""){Label3.Text="请选择上传图片";}

else

{Label3.Text="格式不正确或过大";}

}


//图格式检验4

stringname4=FileUpload4.FileName;

intsize4=FileUpload4.PostedFile.ContentLength;

strings1type4=FileUpload4.PostedFile.ContentType;

strings2type4=name4.Substring(name4.LastIndexOf(".")+1);


if(s1type4=="image/pjpeg"&&size4<112640&&name4!=""||s1type4=="image/gif"&&size4<112640&&name4!="")

{

stringnewName4=DateTime.Now.Year.ToString()+DateTime.Now.Month.ToString()+DateTime.Now.Day.ToString()+DateTime.Now.Hour.ToString()+DateTime.Now.Minute.ToString()+DateTime.Now.Second.ToString()+DateTime.Now.Millisecond.ToString()+"c"+"."+s2type4;

stringimgpath4=Server.MapPath("upfill")+"\\"+newName4;

stringsjpath4="upfill\\"+newName4;

FileUpload4.SaveAs(imgpath4);

Image4.ImageUrl=sjpath4;

Label4.Text="上传成功";

}

else

{

if(name4==""){Label4.Text="请选择上传图片";}

else

{Label4.Text="格式不正确或过大";}

}

}

}

========================================================================

一共上是4张图片一起上传因为是一样的注释我就加一段了中间有一个问题就是如果重新命名的表达式一样的话是无法同时写入多个文件的有好几次都只写了2个,后来才明白是因为名称重复了所以仔细看newName的表达式都是不一样的我加了不同的字母==
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: