我一直得到这个"索引超出了阵列的范围"例外。我有一个带文件名的数据表。连接到tabletable的webform页面上有一个列表框。我试图从列表框中选择多个选项。然后将每个文件名发送到处理程序.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中使用数据表中选定的文件名填充数组,但它不会。它不断提出这个例外。我不知道为什么这不起作用。
答案 0 :(得分:3)
你有:
for (int i = 0; i < a + 1; i++)
应该是:
for (int i = 0; i < a; i++)
答案 1 :(得分:1)
我认为这是你的问题:
for (int i = 0; i < a + 1; i++)
{
string fn = tempDV.Table.Rows[AllselectedIndex[i+1]].ItemArray[1].ToString();
ImgHandler.filename[i] = fn.Trim();
}
将“a + 1”更改为“a”
答案 2 :(得分:0)
string sRecontent = txtrepalcecontent.Text;
string[] fileLineString = new string[sContent.Length];
for (int i =0 ; i < sContent.Length; i++)
{
//fileLineString[0] = sContent[0].ToString();
string[] content = sContent.Split(delim);
if (sFind == content[i]) **//index was outside the bounds the array**
{
string A = sContent.Remove(i, txtfind.Text.Length);
string S = A.Insert(i, sReplace);
txtrepalcecontent.Text = S;
}