asp.net上传图片

protected void BtnOk_Click(object sender, EventArgs e)
    {
        if (this.FileUpload1.FileName == "")
        {
            this.lblMessage.Text = "<font color=#FF0000>请选择上传的图片!</font>";
            this.FileUpload1.Focus();
            return;
        }
        string filePath = this.FileUpload1.FileName;
string fileName = filePath.Substring(filePath.LastIndexOf("\\")+1);
        string fileType = filePath.Substring(filePath.LastIndexOf(".")+1);
        string filePic = Server.MapPath("~/images/" + fileName);
        string[] strArr = new string[] { "gif", "jpg", "bmp" };
        for (int i = 0; i < strArr.Length; i++)
        {
            if (fileType.ToLower() == strArr[i])
            {
                this.FileUpload1.PostedFile.SaveAs(filePic);
                this.lblMessage.Text = "上传图片成功!";
                return;
            }
        }
        this.lblMessage.Text = "您上传的图片格式不正确\n,请上传jpg,gif,bmp格式的图片.";
    }

posted @ 2012-08-29 11:01  dogxuefeng  阅读(186)  评论(0编辑  收藏  举报