ASP.NET同时上传多个图片

来源:互联网 发布:淘宝客招募平台 编辑:程序博客网 时间:2024/05/21 16:28
http://www.cnblogs.com/superfang/archive/2009/01/09/1233814.html
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
public partial class img_upfill : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
 
    }
    protected void Button1_Click(object sender, EventArgs e)
    {
        //案例信息
        string al_name = TextBox1.Text;//需要写入数据库的可以不管
        string al_link = TextBox2.Text;;//需要写入数据库的可以不管
        string al_sx = TextBox3.Text;;//需要写入数据库的可以不管
        int ial_sx = int.Parse(al_sx);;//需要写入数据库的可以不管
        string al_class = DropDownList1.SelectedValue;;//需要写入数据库的可以不管
        int ial_class = int.Parse(al_class);;//需要写入数据库的可以不管
        //logo图片格式检验1
        string name1 = FileUpload1.FileName;//设置变量name1等于上传文件的全路径名称也就是浏览后的内容
        int size1 = FileUpload1.PostedFile.ContentLength;//获取上传文件的大小
        string type1 = FileUpload1.PostedFile.ContentType;//获取上传文件的MIME类型 用语非法格式上传判断
        string type2 = name1.Substring(name1.LastIndexOf(".") + 1);//截取出上传文件的类型名称如gif  jpg重新命名时使用
 
if (type1 == "image/pjpeg" && size1 < 112640 && name1 != "" || type1 == "image/gif" && size1 < 112640 && name1 != "")//判断文件的类型和大小 是否为空 如果为JPG或GIF并且不大110KB就算是合法
        {
            string newName = 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;//设置一个变量为上传时间的年月日时秒.. 做为上传文件的新名称
            string imgpath = Server.MapPath("upfill") + "\\" + newName;//Server获取服务器全路径 (包括新文件名)
            string sjpath = "upfill\\" + newName;//写如数据库的 可以不管
            FileUpload1.SaveAs(imgpath);//上传文件
            Image1.ImageUrl = sjpath;//图片显示
            Label1.Text = "上传成功";//成功提示
        }
        else
        {
            if (name1 == "") { Label1.Text = "请选择上传图片"; }
            else
            { Label1.Text = "格式不正确或过大"; }//假如没有上传或上传的不符合标准在执行
 
        }
        
        //缩略图格式检验2
            string name2 = FileUpload2.FileName;
            int size2 = FileUpload2.PostedFile.ContentLength;
            string s1type1 = FileUpload2.PostedFile.ContentType;
            string s2type2 = name2.Substring(name2.LastIndexOf(".") + 1);
 
            if (s1type1 == "image/pjpeg" && size2 < 112640 && name2 != "" || s1type1 == "image/gif" && size2 < 112640 && name2 != "")
            {
                string newName2 = 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;
                string imgpath2 = Server.MapPath("upfill") + "\\" + newName2;
                string sjpath2 = "upfill\\" + newName2;
                FileUpload2.SaveAs(imgpath2);
                Image2.ImageUrl = sjpath2;
                Label2.Text = "上传成功";
            }
            else
            {
                if (name2 == "") { Label2.Text = "请选择上传图片"; }
                else
                { Label2.Text = "格式不正确或过大"; }
            }
            //缩略图格式检验3
            string name3 = FileUpload3.FileName;
            int size3 = FileUpload3.PostedFile.ContentLength;
            string s1type3 = FileUpload3.PostedFile.ContentType;
            string s2type3 = name3.Substring(name3.LastIndexOf(".") + 1);
 
            if (s1type3 == "image/pjpeg" && size3 < 112640 && name3 != "" || s1type3 == "image/gif" && size3 < 112640 && name3 != "")
            {
                string newName3 = 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;
                string imgpath3 = Server.MapPath("upfill") + "\\" + newName3;
                string sjpath3 = "upfill\\" + newName3;
                FileUpload3.SaveAs(imgpath3);
                Image3.ImageUrl = sjpath3;
                Label3.Text = "上传成功";
            }
            else
            {
                if (name3 == "") { Label3.Text = "请选择上传图片"; }
                else
                { Label3.Text = "格式不正确或过大"; }
            }
          
        //图格式检验4
            string name4 = FileUpload4.FileName;
            int size4 = FileUpload4.PostedFile.ContentLength;
            string s1type4 = FileUpload4.PostedFile.ContentType;
            string s2type4 = name4.Substring(name4.LastIndexOf(".") + 1);
 
            if (s1type4 == "image/pjpeg" && size4 < 112640 && name4 != "" || s1type4 == "image/gif" && size4 < 112640 && name4 != "")
            {
                string newName4 = 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;
                string imgpath4 = Server.MapPath("upfill") + "\\" + newName4;
                string sjpath4 = "upfill\\" + newName4;
                FileUpload4.SaveAs(imgpath4);
                Image4.ImageUrl = sjpath4;
                Label4.Text = "上传成功";
            }
            else
            {
                if (name4 == "") { Label4.Text = "请选择上传图片"; }
                else
                { Label4.Text = "格式不正确或过大"; }
            }
    }
}
========================================================================
一共上是4张图片一起上传  因为是一样的 注释我就加一段了 中间有一个问题就是 如果重新命名的表达式一样的话 是无法同时写入多个文件的 有好几次都只写了2个,后来才明白是因为名称重复了所以仔细看  newName 的表达式都是不一样的 我加了不同的字母==
0 0