• 图片上传


    protected void Upload()
            {
                //判断是否选择文件
                if (FileUpload1.HasFile)
                {
                    string fileContentType = FileUpload1.PostedFile.ContentType;//获取文件类型
                    //判断类型是否符合条件
                    if (fileContentType == "image/bmp" || fileContentType == "image/gif" || fileContentType == "image/pjpeg"||fileContentType=="image/PNG")
                    {
                        //客户端文件路径
                        string name = FileUpload1.PostedFile.FileName;
                        FileInfo file = new FileInfo(name);
                        //文件名称
                        string fileName = file.Name;
                        //服务器端文件路径
                        string webFilePath = Server.MapPath("resource/" + fileName);
                        //判断同名文件是否存在
                        if (!File.Exists(webFilePath))
                        {
                            try
                            {
                                //使用SaveAs方法保存文件
                                FileUpload1.SaveAs(webFilePath);
                                this.lMsg.Text = "提示:文件" + fileName + "上传成功!";
                                this.lPathInfo.Text = "resource" + fileName;
                                this.iPic.ImageUrl = "resource" + fileName;
                            }
                            catch (Exception ex)
                            {
                                this.lMsg.Text = "提示:文件上传失败,失败原因:" + ex.Message;
                            }
                        }
                        else
                        {
                            this.lMsg.Text = "提示:文件已经存在,请重命名后上传";
                        }
                    }
                    else
                    {
                        this.lMsg.Text = "提示:文件类型不符合";
                    }
                }
            }
            protected void btUpload_Click(object sender, EventArgs e)
            {
                if (Directory.Exists("resource"))
                {
                    Upload();
                }
                else
                {
                   
                    Directory.CreateDirectory(Server.MapPath("resource"));
                    Upload();
                }
               
            }
    感谢来访,共同学习!
  • 相关阅读:
    BZOJ BLO 1123 (割点)【双连通】
    P4291 [HAOI2008]排名系统
    P3165 [CQOI2014]排序机械臂
    P3224 [HNOI2012]永无乡
    P1169 [ZJOI2007]棋盘制作
    P2303 [SDOi2012]Longge的问题
    P2216 [HAOI2007]理想的正方形
    P2473 [SCOI2008]奖励关
    P2617 Dynamic Rankings
    P2518 [HAOI2010]计数
  • 原文地址:https://www.cnblogs.com/dingxiaowei/p/3058837.html
Copyright © 2020-2023  润新知