下面是详细的错误描述:问题关键是我本地调试没有错误,将应用程序发布到服务器上后即报错,是何原因呢?“/SPbulletin_System”应用程序中的服务器错误。 报错的部分代码:// 获取上传图片的文件名
        String fileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
      //  String fileName = FileUpload1.PostedFile.FileName.ToString();
        //this.Image1.ImageUrl = Request.ApplicationPath + "\\Pictures" + fileName;
        if (System.IO.File.Exists(Server.MapPath(Request.ApplicationPath) + "\\Pictures"  + fileName))
        {
            Response.Write("<script>window.alert('该文件名在服务器中已存在,请更改文件名!')</script>");
        }
        else
        {
            try
            {                // 上传文件
                FileUpload1.PostedFile.SaveAs(Server.MapPath(Request.ApplicationPath) + "\\Pictures" + fileName);
                // 添加数据库信息
                //addNode(int eventid,string eventtopic,string eventtxt,string Imagname,string Imagurl,string EventStayTime,string ImagStayTime,string ImagID,bool whetheraudit)
                //"INSERT INTO eventpost (EventID,EventTopic,EventTxt,ImgName,ImgUrl,EventPostDate,EventStayTime,ImagStayTime,EventImgID,whetheraudit)VALUES('" + eventid + "','" + eventtopic + "','" + eventtxt + "','" + Imagname + "','" + Imagurl + "',GetDate(),'" + EventStayTime + "','" + ImagStayTime + "','" + ImagID + "','" + whetheraudit + "'')";
                //SQLHelper helper = new SQLHelper();
                helper.addNode(eventid, eventtopic1, eventtxt1, fileName, Request.ApplicationPath + "\\Pictures"  + fileName, EventStayTime.Text, TextBox7.Text, imagid1, whetheraudit);
                // 显示上载成功的信息
                Response.Write("<script>window.alert('文件上传成功!')</script>");
            }
            catch (Exception ex)
            {
                Response.Write("<script>window.alert('由于网络原因,上载文件错误  " + ex.Message + "')</script>");
            }
________________________________________
StartIndex 不能小于 0。
参数名: startIndex 
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。 异常详细信息: System.ArgumentOutOfRangeException: StartIndex 不能小于 0。
参数名: startIndex源错误: 行 205:
行 206:        // 获取上传图片的文件名
行 207:        String fileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
行 208:      //  String fileName=FileUpload1.PostedFile.FileName.ToString();
行 209:        //this.Image1.ImageUrl = Request.ApplicationPath + "\\Pictures" + fileName;源文件: e:\SPbulletin_System\eventpost.aspx.cs    行: 207 堆栈跟踪: [ArgumentOutOfRangeException: StartIndex 不能小于 0。
参数名: startIndex]
   System.String.InternalSubStringWithChecks(Int32 startIndex, Int32 length, Boolean fAlwaysCopy) +7491311
   System.String.Substring(Int32 startIndex) +17
   eventpost.fileupload_post1() in e:\SPbulletin_System\eventpost.aspx.cs:207
   eventpost.Button1_Click(Object sender, EventArgs e) in e:\SPbulletin_System\eventpost.aspx.cs:97
   System.Web.UI.WebControls.Button.OnClick(EventArgs e) +111
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +110
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +36
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1565________________________________________
版本信息: Microsoft .NET Framework 版本:2.0.50727.3603; ASP.NET 版本:2.0.50727.3053

解决方案 »

  1.   

    报错行 207: String fileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
      

  2.   

    String fileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));可能FileUpload1.PostedFile.FileName.LastIndexOf("\\")没有找到,为-1所以挂了
      

  3.   

    LastIndexOf()方法未找到字符"\",返回值-1
    substring()参数不能为负数
      

  4.   

    String fileName = FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
    尽量不要写这种代码,效率并不高,
    却容易出错,也不方便调试和阅读!!
    如下拆开来写,调试下就好啦
    var tmpA = FileUpload1.PostedFile.FileName.LastIndexOf("\\");
    String fileName = FileUpload1.PostedFile.FileName.Substring(tmpA);
      

  5.   

    String fileName ="";
    if(FileUpload1.PostedFile.FileName.LastIndexOf("\\")>0)
    {
     fileName=FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
    }
    你先判断一下啊。要是没找到就别赋值了。
      

  6.   

    好的,这个判断我会加上的,但为什么服务器上取出的LastIndexOf()会是-1,本地能正常取出值来?
      

  7.   

    filename中没有"\\"   加个判断先
      

  8.   

    我的操作过程是这样的,在本地打开程序发布的地址,然后从本地找一个图片,执行上传程序时报错。
    我在本地调试不报错,直接到服务器上打开程序上传本地图片不报错,服务器我有administrator权限。
      

  9.   

    等于是服务器无法读取本地browser上的上传的本地图片路径。
      

  10.   

    我的操作过程是这样的,在本地打开程序发布的地址(服务器发布的地址),然后从本地找一个图片,执行上传程序时(此时程序执行的是服务器上的)报错。我在本地调试(数据库、程序都在本地)不报错,直接到服务器上打开(服务器上发布的)程序,上传服务器本地的图片不报错,服务器我有administrator权限。
      

  11.   

    很清楚了,问题比较奇怪了。你打开那个程序都是用的ie打开的吧?
    如果不是的,看看iis的权限。如果是的,那我就想不到原因了。
      

  12.   

    是用ie打开的,iis我设置在服务器上的是虚拟目录,已经加了everyone 所有操作的权限了,另外还有我的程序使用了母版,会不会跟这个有关系呢?
      

  13.   

    那有没有什么替代substring(lastindexof)的代码,其实我就是想取到文件名称并且前面带个"\\"(后面在使用图片路径时需要用到)。
      

  14.   

    如果报的错是startindex的那个错,是因为没找到对应的文件路径,和母版应该没有关系。毕竟fileupload读的路径都是本地路径。
      

  15.   

    /// <summary>
        /// 上传图片
        /// </summary>
        private void UpDown()
        {
            string imgName = fileImage.FileName; ;
            if (imgName != "")
            {
                string paths = AppDomain.CurrentDomain.BaseDirectory.ToString() + "images\\Product\\";
                if (!Directory.Exists(paths))
                {
                    Directory.CreateDirectory(paths);
                }            //上传
                bool falg = ValidatorPic(imgName);
                if (falg)
                {
                    string imgPath = hfImg.Value;
                    if (imgPath == "")
                    {
                        imgPath = "g_" + System.DateTime.Now.ToString("yyyyMMddHHmmss") + imgName.Substring(imgName.LastIndexOf("."), imgName.Length - imgName.LastIndexOf("."));
                    }
                    //覆盖原有图片
                    fileImage.PostedFile.SaveAs(paths + imgPath);
                    this.imgGoods.ImageUrl = "~/images/Product/" + imgPath;
                    hfImg.Value = imgPath;
                }
                else
                {
                    JavaScriptAlert("操作提示\\n格式不正确!");
                }
            }
            else
            {
                JavaScriptAlert("操作提示\\n请选择图片!");
            }    }你看看我以前用的上传的方法吧。