我用下面的方式上传图片后,发现图片存贮在根目录里,我新建一个image文件夹,希望上传的图片能集中存放在这个文件中,如何修改下列代码?
请高手指点,谢谢!
private string image_upload()
    {
        if (imageUpload.HasFile)
        {
            string strimagename = imageUpload.FileName;
            string strimageExt = System.IO.Path.GetExtension(strimagename);
            if (strimageExt == "")
            {
                Response.Write("<script>alret('系统只接受.jpg或.gif')</script>");
                return "";
            }
            else
                if (strimageExt.ToLower() == ".jpg" || strimageExt.ToLower() == ".gif")
                {
                    imageUpload.SaveAs(Server.MapPath(strimagename));
                    return strimagename;
                }
                else
                {
                    Response.Write("<script>alret('系统只接受.jpg或.gif')</script>");
                    return "";
                }
        }
        else
        {
            Response.Write("<script>alret('文件不存在')</script>");
            return "";
        }
    }

解决方案 »

  1.   

    如这样子
    string tempImgPath="~/tempImgPath";
    string path=Path.Combine(tempImgPath,strimagename);
    imageUpload.SaveAs(Server.MapPath(path));试下
      

  2.   

    path=Server.MapPath("~/");
    //imageUpload.SaveAs(path+"前面是你的网站根目录,这就随你写了");
    如:imageUpload.SaveAs(path+"images"); 
      

  3.   

    imageUpload.SaveAs(Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/image/" + strimagename));
      

  4.   

                        string tempImgPath = "~/bookimage";
                        string path = Path.Combine(tempImgPath, strimagename);
                        imageUpload.SaveAs(Server.MapPath(path)); 
                        //imageUpload.SaveAs(Server.MapPath(strimagename));
                        return strimagename;提示错误:当前上下文中不存在Path,(红字部分)
      

  5.   

    把上传图片放在指定的文件夹中最后解决方案:imageUpload.SaveAs(Server.MapPath(HttpContext.Current.Request.ApplicationPath + "/image/" + strimagename));赠分已送!虽然其他几位的方案没采纳,在此也对大家的热情深表感谢!