.net上传功能fileupload代码

  protected void Button4_Click(object sender, EventArgs e)
        {
            /*  所有文件全部通通都上传
            string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + FileUpload1.FileName;
            FileUpload1.SaveAs(Server.MapPath("touxiang\\" + fn));

             */

            this.Image1.Visible = false;
            string fn = DateTime.Now.ToString("yyyyMMddHHmmss") + FileUpload1.FileName;

            if (FileUpload1.HasFile)
            {
                string fileExt = System.IO.Path.GetExtension(FileUpload1.FileName);
                if (fileExt == ".jpg" || fileExt == ".gif" || fileExt == ".bmp")
                {
                    try
                    {
                        FileUpload1.SaveAs(Server.MapPath("/touxiang") + "\\" + fn);
                        this.Image1.Visible = true;
                        this.Image1.ImageUrl = "~/touxiang" + "/" + fn;
 
                    }
                    catch (Exception ex)
                    {
                        LTP.Common.MessageBox.Show(this, "发生错误:" + ex.Message.ToString());
                    }
                }
                else
                {
                    LTP.Common.MessageBox.Show(this, "只允许上传jpg、gif文件!");
                }
            }
            else
            {
                LTP.Common.MessageBox.Show(this, "没有选择要上传的文件!");
            }


        }

posted @ 2012-09-27 21:21  几秒钟前  阅读(146)  评论(0编辑  收藏  举报