ASP.NET中FileUpload中的代码怎么编写?

protected void submit_Click(object sender, EventArgs e)
    {
        string name = FileUpload1.FileName;
        int size = FileUpload1.PostedFile.ContentLength;
        string type = FileUpload1.PostedFile.ContentType;             //image/pjpeg
        // string type2 = name.Substring(name.LastIndexOf(".") + 1);     //jpg 不安全
        string ipath = Server.MapPath(@"~\img\") + name;
        string dpath = @"~\img\" + name;
        if (type.Contains("image"))
        {
            if (size < 1048576)    // 1MB
            {
                FileUpload1.SaveAs(ipath);
                Image1.Visible = true;
                Image1.ImageUrl = dpath;
                Response.Write("恭喜你上传成功!");
            }
            else
            {
                Response.Write("图片过大!请上传小于1MB的图片");
            }
        }
        else
        {
            Response.Write("请上传正确的图片格式");
        }
       
    }

posted @ 2012-05-23 21:35  摩根船长  阅读(169)  评论(0编辑  收藏  举报