下面是详细的错误描述:问题关键是我本地调试没有错误,将应用程序发布到服务器上后即报错,是何原因呢?“/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
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
substring()参数不能为负数
尽量不要写这种代码,效率并不高,
却容易出错,也不方便调试和阅读!!
如下拆开来写,调试下就好啦
var tmpA = FileUpload1.PostedFile.FileName.LastIndexOf("\\");
String fileName = FileUpload1.PostedFile.FileName.Substring(tmpA);
if(FileUpload1.PostedFile.FileName.LastIndexOf("\\")>0)
{
fileName=FileUpload1.PostedFile.FileName.Substring(FileUpload1.PostedFile.FileName.LastIndexOf("\\"));
}
你先判断一下啊。要是没找到就别赋值了。
我在本地调试不报错,直接到服务器上打开程序上传本地图片不报错,服务器我有administrator权限。
如果不是的,看看iis的权限。如果是的,那我就想不到原因了。
/// 上传图片
/// </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请选择图片!");
} }你看看我以前用的上传的方法吧。