每当我在UploadComplete()中获得一个文件名时,它会在加载下一个文件时丢失,因为每个文件加载都会导致自动回发。我试图阻止它,但它不起作用。我还尝试使用ViewState来保存仍无法正常工作的文件名。如何保留上传文件名列表?
答案 0 :(得分:0)
您可以通过这种方式将它们保存到会话对象中。列表加载会话数据,添加新项目,会话更新列表。通过这样做,会话将始终在每次上载后保留数据,而不是替换现有的数据。
<强> C#强>
//global declaration
private List<string> UploadList;
protected void AjaxFileUpload1_UploadComplete()
{
String fileName = IO.Path.GetFileName(e.FileName);
UploadList = Session["UploadedFiles"];
UploadList.Add(fileName);
Session["UploadedFiles"] = UploadList;
}
//retrieve the items from list
private void GetList()
{
UploadList = Session["UploadedFiles"];
//loop through the list or access each item by the index
}
VB
'global declaration
Dim UploadList as List(Of String)
Protected Sub AjaxFileUpload1.UploadComplete()
Dim fileName = IO.Path.GetFileName(e.FileName)
UploadList = Session("UploadedFiles")
UploadList.Add(fileName)
Session("UploadedFiles") = UploadList
End Sub
'retrieve the items from list
Private Sub GetList()
UploadList = Session("UploadedFiles")
'loop through the list or access each item by the index
End Sub