请帮我看看,上传文件哪里出错了!

szdotnet 2004-05-12 03:26:49
不知道为什么,不能上传文件,源文件如下:


private void Button1_Click(object sender, System.EventArgs e)
{
HttpFileCollection MyFileCollection = Request.Files;
string path = Server.MapPath("./");
MyFileCollection[0].SaveAs(path);
}


错误信息如下:

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentOutOfRangeException: Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index

Source Error:

Line 53: string path = Server.MapPath("./");
Line 54: MyFileCollection[0].SaveAs(path);
Line 55: }
Line 56: }


Source File: c:\inetpub\wwwroot\webapplication1\webform1.aspx.cs Line: 54
...全文
119 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
szdotnet 2004-05-12
  • 打赏
  • 举报
回复
vzxq(灵感人) 能够把你的源文件发过来吗?
sz_dotnet@yahoo.com.cn

谢谢!
vzxq 2004-05-12
  • 打赏
  • 举报
回复
if(Tb_TrunFile.PostedFile == null|| Tb_TrunFile.PostedFile.FileName.Trim().Length == 0)
{
images_Message.Text = "请选择要上传的源文件!";
return ;
}

HttpPostedFile myFile = Tb_TrunFile.PostedFile;
string SourFile = Tb_TrunFile.PostedFile.FileName; //返回的是通过文件对话框选择的文件名
string images_Name = Path.GetFileName(SourFile);//' 去掉目录信息,返回文件名称
string newext=System.IO.Path.GetExtension(myFile.FileName).ToLower();
//if((System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".jpg") || (System.IO.Path.GetExtension(myFile.FileName).ToLower() != ".bmp")||(System.IO.Path.GetFileName(myFile.FileName).ToLower() != ".gif"))
//int i= images_Name.LastIndexOf(".");
//--获得扩展名
//string newext =images_Name.Substring(i);
if(newext.Equals(".jpg")==false && newext.Equals(".bmp")==false && newext.Equals(".gif")==false)
{
LabelMessage.Text="The file must have an extension of jPG or BMP or GIG";
return;
}
//string DestPath =@"C://Inetpub//wwwroot//center//image//";
string DestPath=@"D://center_new_spieler_0331//image//";
string test=DestPath.Trim() + images_Name;
DestFile[images_news_test]=test;//生成目的文件路径

if(DestFile[images_news_test].Trim().Length == 0)
{
images_Message.Text = "请输入要上传的目的文件!";
return ;
}

if(!Directory.Exists(DestPath))
{
Directory.CreateDirectory(DestPath);
}
Tb_TrunFile.PostedFile.SaveAs(DestFile[images_news_test]);

DestFile[images_news_test]=images_Name;
images_list.Items.Add(DestFile[images_news_test]);
images_news_test++;
images_Message.Text="第"+images_news_test+"张图片上传成功";
}
catch(Exception ex)
{
images_Message.Text=ex.Message+"上传出错";
}
lxcc 2004-05-12
  • 打赏
  • 举报
回复
up
vzxq 2004-05-12
  • 打赏
  • 举报
回复
写上绝对径行不行
szdotnet 2004-05-12
  • 打赏
  • 举报
回复
baddot(小不点) ,把“/”去掉。

string path = Server.MapPath(".");


仍然不行,同样的错误!
szdotnet 2004-05-12
  • 打赏
  • 举报
回复
即使把path 换掉,如下:
string TempFileName;
TempFileName = "C:\\TempFiles\\";
MyFileCollection[0].SaveAs(TempFileName);

仍然会出现同样的问题!
baddot 2004-05-12
  • 打赏
  • 举报
回复
把“/”去掉。

string path = Server.MapPath(".");
极速小王子 2004-05-12
  • 打赏
  • 举报
回复
是不是需要指定文件名啊?
极速小王子 2004-05-12
  • 打赏
  • 举报
回复
string path = Server.MapPath("./");
中的path值是什么啊?

62,071

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术交流专区
javascript云原生 企业社区
社区管理员
  • ASP.NET
  • .Net开发者社区
  • R小R
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

.NET 社区是一个围绕开源 .NET 的开放、热情、创新、包容的技术社区。社区致力于为广大 .NET 爱好者提供一个良好的知识共享、协同互助的 .NET 技术交流环境。我们尊重不同意见,支持健康理性的辩论和互动,反对歧视和攻击。

希望和大家一起共同营造一个活跃、友好的社区氛围。

试试用AI创作助手写篇文章吧