我有以下 C# 代码:
string File = "../images/main/profile-icon.png";
if (Request.ContentLength != 0)
{
int Size = Request.Files[0].ContentLength / 1024;
if (Size <= 512)
{
string LocalFile = Request.Files[0].FileName;
int dot = LocalFile.LastIndexOf('.');
string FileType = LocalFile.Substring(dot + 1);
if (FileType == "gif" || FileType == "jpg" || FileType == "png" || FileType == "GIF" || FileType == "JPG" || FileType == "PNG")
{
int LastIndex = LocalFile.LastIndexOf(@"\") + 1;
File = LocalFile.Substring(LastIndex, LocalFile.Length - LastIndex);
File = DateTime.Today.ToString();
string Path = Server.MapPath(" ../images/profiles") + "..\\" + File;
Request.Files[0].SaveAs(Path);
}
}
else
{
Response.Write("The file is too big !");
}
}
else
{
Response.Write("Unknown Error !");
}
问题是在第三个代码行中我收到以下错误:
指数超出范围。必须是非负数且小于集合的大小。
这是表单 HTML 源代码:
<form name="Register" runat="server" style="margin-top: 15px;" onsubmit="return validateProfile('Register');">
<p>
photo:
<input type="file" name="File" style="margin-right:10px;" />
</p>
</form>
我的问题是为什么以及如何解决这个问题?
希望得到帮助,谢谢!