• C#.net实现图片上传功能


    C#.NET前台:<asp:Image ID="imgFood" runat="server" />
               <asp:FileUpload ID="fileUpLoadPic" runat="server" /> 
               <asp:Button ID="Button1" runat="server" Text="上传" 
                                onclick="btnPicUpload_Click"/>
    后台.CS文件:
      protected void btnPicUpload_Click(object sender, EventArgs e)  //上传按钮
        { 
             if (fileUpLoadPic.HasFile) //文件存在
            {
                SaveFile(fileUpLoadPic.PostedFile);//保存上传文件
            }
            else
            {
                Response.Write("<script>alert('上传文件不存在!')</script>");
            }
    
            if (fileUpLoadPic.PostedFile.FileName == "")  //文件名字
            {
                Response.Write("<script>alert('你还没有选择图片!')</script>");
            }
            else
            {
                string filepath = fileUpLoadPic.PostedFile.FileName;
                string filename = filepath.Substring(filepath.LastIndexOf("\") + 1);//第一个转义字符
                Session["filename"] = filename;
                string fileEx = filepath.Substring(filepath.LastIndexOf(".") + 1);//从.开始截至最后得到图片格式.jpg。。。
                string serverpath = Server.MapPath("\images\") + filename; 
                if (fileEx == "jpg" || fileEx == "bmp" || fileEx == "gif")
                {
                    imgFood.ImageUrl = "images/" + filename;
                    Response.Write("<script>alert('上传成功!')</script>");
                    return;
                }
                else
                {
                    Response.Write("<script>alert('上传的格式有问题!')</script>");
                    return;
                }
            }
        }
     public void SaveFile(HttpPostedFile file)
        {
            string savePath = "C:\Users\DJJ\Desktop\School_Canteen2\Web\images\";
            string fileName = fileUpLoadPic.FileName;
    
            string pathToCheck = savePath + fileName;
            string tempfilename = "";
            if (System.IO.File.Exists(pathToCheck))
            {
                int counter = 2;
                while (System.IO.File.Exists(pathToCheck))
                {
                    tempfilename = counter.ToString() + fileName;
                    pathToCheck = savePath + tempfilename;
                    counter++;
                }
                fileName = tempfilename;
                Response.Write("<script>alert('你上传了两个相同文件!')</script>");
            } 
            savePath += fileName;
            fileUpLoadPic.SaveAs(savePath);
        }
    若前台要保存图片路径到数据库:获取路径方式 (以string数据类型保存)
      string F_Pic = Convert.ToString(Session["filename"]);//this.fileUpLoadPic.FileName;//fileUpLoadPic.AppRelativeTemplateSourceDirectory;//图片的数据库中保存图片路径
      string f_pic = F_Pic.Substring(0, F_Pic.LastIndexOf(".")); //得到去掉.jpg后的名字
    

    选择文件后,点击上传即可,上述代码虽有提示上传相同图片的功能,但还是会上传,若不想上传要做具体处理。
    
  • 相关阅读:
    运营总监招聘-e袋洗招聘-拉勾网
    中国服饰行业十大趋势
    赢在形象力之色彩
    百度系统部 在 北京市海淀区西二旗首创空间大厦 招聘 Python-交付运维系统研发工程师
    时间规划师
    使用python/casperjs编写终极爬虫-客户端App的抓取-ZOL技术频道
    传统线下零售商已经过时了,细分电商领域的机会仍待挖掘 | 36氪
    艺术私学----免费摄影、绘画、时尚造型课程体验_豆瓣
    艺术私学------绘画免费体验课程_豆瓣
    艺术私学------绘画免费体验课程_豆瓣
  • 原文地址:https://www.cnblogs.com/dingfangbo/p/5769949.html
Copyright © 2020-2023  润新知