"인덱스가 배열의 경계를 벗어났습니다"예외가 계속 발생합니다. 나는 파일 이름을 가진 datatable을 가지고있다. 웹 양식 페이지에는 태블릿에 연결된 목록 상자가 있습니다. 목록 상자에서 여러 옵션을 선택하려고합니다. 그런 다음 각 파일 이름을 처리기 .ashx 파일로 보냅니다. 내가 볼 수있는 for 루프를 사용하고있다. 여기 "인덱스가 배열의 경계를 벗어났습니다"라는 예외를 수정하는 방법은 무엇입니까?
는 main.aspx.cs 코드 :namespace MultiImagesImageHandler
{
public partial class Main : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
int[] AllselectedIndex = ListBox1.GetSelectedIndices();
int a = AllselectedIndex.Length;
DataView tempDV = SqlDataSource1.Select(DataSourceSelectArguments.Empty) as DataView;
for (int i = 0; i < a + 1; i++)
{
string fn = tempDV.Table.Rows[AllselectedIndex[i+1]].ItemArray[1].ToString();
ImgHandler.filename[i] = fn.Trim();
}
Response.Redirect("image1.htm");
}
}
}
handler.ashx.cs 코드 :
namespace MultiImagesImageHandler
{
/// <summary>
/// Summary description for ImgHandler
/// </summary>
public class ImgHandler : IHttpHandler
{
public static string[] filename = new string[16];
public void ProcessRequest(HttpContext context)
{
string saveTo = @"fileLocation" + filename[3];
FileStream writeStream1 = new FileStream(saveTo, FileMode.OpenOrCreate, FileAccess.ReadWrite);
string loadFrom = @"fileLocation" + filename[3];
using (FileStream fs1 = File.Open(loadFrom, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite))
{
ReadWriteStream(fs1, writeStream1);
}
byte[] tt = File.ReadAllBytes(context.Server.MapPath("~/images/" + filename[3]));
context.Response.ContentType = "image/jpeg";
context.Response.BinaryWrite(tt);
}
public bool IsReusable
{
get
{
return false;
}
}
// readStream is the stream you need to read
// writeStream is the stream you want to write to
private void ReadWriteStream(Stream readStream, Stream writeStream)
{
int Length = 256;
Byte[] buffer = new Byte[Length];
int bytesRead = readStream.Read(buffer, 0, Length);
// write the required bytes
while (bytesRead > 0)
{
writeStream.Write(buffer, 0, bytesRead);
bytesRead = readStream.Read(buffer, 0, Length);
}
readStream.Close();
writeStream.Close();
}
}
}
나는이와 ImgHandler.ashx.cs의 배열을 채울 것이라고 생각 선택한 파일 이름은 데이터 테이블에서 가져 왔지만 실제로는 그렇지 않습니다. 이 예외는 계속 발생합니다. 왜 이것이 작동하지 않는지 나는 모른다.
for (int i = 0; i user1274820
이'i
바깥에 나가서 for 루프를 +1하면 안됩니다. – Sybren