ASP.NET 上传控件问题
我做了一个上传控件,但运行出错,提示如下所示:
“/myweb”应用程序中的服务器错误。 StartIndex 不能小于 0。
参数名: startIndex
说明: 执行当前 Web 请求期间,出现未处理的异常。请检查堆栈跟踪信息,以了解有关该错误以及代码中导致错误的出处的详细信息。
异常详细信息: System.ArgumentOutOfRangeException: StartIndex 不能小于 0。
参数名: startIndex
代码如下:
if (FileUpload1.PostedFile != null)
FileUpload1.Visible = true;
string strdir = FileUpload1.PostedFile.FileName;
int mypos = strdir.LastIndexOf("\\");
string strfilename = strdir.Substring(mypos);
string strpath = Server.MapPath(".") + strfilename;
this.Label2.Text = "文件名称:";
this.Label2.Text += FileUpload1.PostedFile.FileName;
this.Label3.Text = "保存路径:";
this.Label3.Text += strpath;
this.Label4.Text = "文件类型:";
this.Label4.Text += FileUpload1.PostedFile.ContentType;
this.Label5.Text = "文件大小:";
this.Label5.Text += FileUpload1.PostedFile.ContentLength.ToString();
请问怎么解决,具体在哪改?
追问:兄弟你太牛了,能告诉我原因吗?你的扣扣可以告诉我吗?
答案: string strdir = FileUpload1.PostedFile.FileName;
int mypos = strdir.LastIndexOf("\\");
string strfilename = strdir.Substring(mypos);
string strpath = Server.MapPath(".") + strfilename;这4句改成这样
string filename= FileUpload1.FileName;
string strpath = Server.MapPath(".") +"\\"+ filename;
定然不会出错的
这个在网上我没有找到,你要是找到可以给我发一个吗?不过我自己用c#写了一个图片上传工具,上传然后显示
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Drawing;
public partial class shiyan : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
MsgFind.Text=" ";
Label1.Text=" ";
}
protected void Button1_Click(object sender, EventArgs e)
{
int i;
int u_FileNum = 0, f_FileNum = 0, e_FileNum = 0, s_FileNum = 0;
String u_FileName = "上传成功:";
String f_FileName = "文件不存在:";
String e_FileName = "文件名重复:";
String s_FileName = "文件超过1000K:";
for (i = 0; i < Request.Files.Count; i )
{
HttpPostedFile upFile = Request.Files[i]; //获得上传文件信息到内存中
String[] Route = upFile.FileName.Split(Convert.ToChar("\\"));//由"\"符号将上传的文件路径截取成若干字符串
String FileName = Route[Route.Length - 1];//截取的最后一个字符串即为文件名
//例如上传文件路径为 E:\图片\picture1, 则截取的字符串分别为 "E:","图片","picture1",所以最后一个字符串“picture1"即为文件名
if (upFile.ContentLength > 0) //如果文件大小大于0
{
if (File.Exists(Server.MapPath("shiyan\\") FileName))//判断目标文件夹是否存在同名文件
{
e_FileNum = 1; //存在,同名文件数加1
e_FileName = e_FileName "[" FileName "] ";//记录同名的文件名
}
else if (upFile.ContentLength > 102400) //判断上传文件大小,在这默认的上传文件大小为小于1000k
{
s_FileNum = 1; //超过大小的文件数加1
s_FileName = s_FileName "[" FileName "] ";//记录超过大小的文件名
}
else
{
Image1.Visible = true; //图片预览image可以显示
upFile.SaveAs(Server.MapPath("shiyan\\") FileName);// 将上传文件从内存中存入目标文件夹
u_FileNum = 1; //上传成功文件数加1
u_FileName = u_FileName "[" FileName "] ";//记录上传成功文件名
string strurl ="~/shiyan/" FileName; //记录上传文件的目标路径
Label1.Text = strurl; //显示上传目标路径
Image1.ImageUrl =strurl; //显示上传图片
}
}
else if (FileName != "") //文件不存在
{
f_FileNum = 1;
f_FileName = f_FileName "[" FileName "] ";
}
}//输出上传结果
if (f_FileNum != 0)
{
MsgFind.Text = MsgFind.Text f_FileName "<br />";
}
if (e_FileNum != 0)
{
MsgFind.Text = MsgFind.Text e_FileName "<br />";
}
if (s_FileNum != 0)
{
MsgFind.Text = MsgFind.Text s_FileName "<br />";
}
if (u_FileNum != 0)
{
MsgFind.Text = MsgFind.Text u_FileName;
}
MsgFind.Visible = true;
}
}
这是程序代码,不能直接复制执行,你可以参照
异常详细信息: System.ArgumentOutOfRangeException: StartIndex 不能小于 0。
参数名: startIndex。
这个错误很明显啊,就是你的下标从0开始了。这是错误的。
上一个:asp中CheckBoxList的问题
下一个:asp.net SqlDataSource的疑问