private string uploadPic()
    {
        string ImageURL = "";
        //检测一下uploadFile是否选择了上传文件
        if (uploadFile.HasFile)
        {
            //检测一下目录是否存在
            if (!Directory.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//"))
            {
                Directory.CreateDirectory(HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//");
            }
            string savePath = @"\upImages\adv\";
            if (uploadFile.HasFile)
            {
                savePath += uploadFile.FileName;
            }
            if (!File.Exists(HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//" + uploadFile.FileName))
            {
                string[] allowesExtensions = { ".gif", ".png", ".bmp", ".jpg" };
                string fileEctension = System.IO.Path.GetExtension(uploadFile.FileName).ToLower();
                bool fileOK = false;
                for (int i = 0; i < allowesExtensions.Length; i++)
                {

                    if (fileEctension == allowesExtensions[i])
                    {

                        fileOK = true;

                    }

                }
                if (!fileOK)
                {
                    lblPicMsg.Visible = true;
                    lblPicMsg.Text = "不支持该类型的文件";
                }
                else
                {
                    if (uploadFile.FileContent.Length / 1024 > 4096)
                    {
                        lblPicMsg.Visible = true;
                        lblPicMsg.Text = "上传文件大小限制为 4MB !";
                    }
                    else
                    {
                        string AutoName = "GTT" + DateTime.Now.ToShortDateString().Replace("-", "") + DateTime.Now.Hour.ToString() + DateTime.Now.Minute.ToString() + DateTime.Now.Second.ToString() + DateTime.Now.Millisecond.ToString() + System.IO.Path.GetExtension(uploadFile.FileName).ToString();
                        string path = Server.MapPath("~") + @"\upImages\adv\" + AutoName;
                        uploadFile.PostedFile.SaveAs(path);
                        lblPicMsg.Visible = true;
                        lblPicMsg.Text = "文件上传成功!" + "<br>";
                        lblPicMsg.Text = lblPicMsg.Text + "文件名称:" + uploadFile.FileName + "<br>";
                        lblPicMsg.Text = lblPicMsg.Text + "文件类型:" + System.IO.Path.GetExtension(uploadFile.FileName).ToString() + "<br>";
                        lblPicMsg.Text = lblPicMsg.Text + "文件大小:" + uploadFile.FileContent.Length / 1024 + " KB" + "<br>";
                        ImageURL = @"\upImages\adv\" + AutoName;
                        //将预览图层内的图片控件显示,并将生成的缩略图地址绑定到该图片上
                        showimg.Visible = true;
                        showimg.Src = HttpContext.Current.Request.PhysicalApplicationPath + "upImages//adv//" + AutoName;
                        //清除上传图片后http数据缓冲
                        Response.Clear();
                    }
                }
            }
            else
            {
                lblPicMsg.Text = uploadFile.FileName + "文件已经存在,重新上传附件!";
            }
        }
        else
        {
            lblPicMsg.Visible = true;
            lblPicMsg.Text = "请选择上传文件 !";
        }
        ImageURL = ImageURL.Replace("\\", "/");
        return ImageURL;
    }

 

posted on 2013-08-13 09:26  王洪洪  阅读(501)  评论(0编辑  收藏  举报