2017-05-30 13 views
0

私はdropzoneを使用して複数のファイルをサーバーにアップロードしています。ファイルはサーバーにアップロードされ、ファイル名はテーブルに保存されます。単一セッション変数内に複数の値を格納および取得する方法

セッション中にファイル名を追加しようとしています。 ここでの問題は、それが単一セッション内で複数のファイル名を追加していないということです

ここに私のコードです:ここ

string imageSessList = context.Session["imageNames"].ToString(); //if i put this line at the begining, then the debugger doesn't even moves to foreach block 


    foreach (string s in context.Request.Files) 
    { 
     HttpPostedFile file = context.Request.Files[s]; 
     string fileName = file.FileName; 
     string fileExtension = file.ContentType; 
     string strUploadFileExtension = fileName.Substring(fileName.LastIndexOf(".") + 1); 
     string strAllowedFileTypes = "***jpg***jpeg***png***gif***bmp***"; //allowed file types 
     string destFileName = ""; 
     List<string> lstImageNames = new List<string>(); 




     // else upload file 
     if (!string.IsNullOrEmpty(fileName)) 
     { 
      if (strAllowedFileTypes.IndexOf("***" + strUploadFileExtension + "***") != -1) //check extension 
      { 
       if (context.Request.Files[0].ContentLength < 5 * 1024 * 1024) //check filesize 
       { 
        // generate file name 
        destFileName = Guid.NewGuid().ToString() + "." + strUploadFileExtension; 
        string destFilePath = HttpContext.Current.Server.MapPath("/resourceContent/") + destFileName; 
        //Save image names to session 
        lstImageNames.Add(destFileName); 
        context.Session["imageNames"] = lstImageNames; 
        file.SaveAs(destFilePath); 

        strMessage = "Success " + destFileName; 
       } 
       else 
       { 
        strMessage = "File Size can't be more than 5 MB."; 
       } 
      } 
      else 
      { 
       strMessage = "File type not supported!"; 
      } 
     } 
    } // foreach 
context.Response.Write(strMessage); 
} 

iは、複数のない、セッションにのみ、単一のファイル名を追加することができていますが。今そこに新しい項目を追加 context.Session [「imageNames」]

答えて

1

セッション

List<string> lstImageNames= (List<string>)Session["imageNames"]; 
if(lstImageNames==null) 
    lstImageNames = new List<string>(); // create new list in the first time 

から現在のリストを取得する必要がありますどのように単一のセッションで複数のファイル名を格納し、維持するために

。セッションを読み取る&lstImageNames.Add(destFileName)に追加した後に戻ってセッションに

context.Session["imageNames"] = lstImageNames; 
+0

設定

lstImageNames.Add(destFileName); 

。 次の行には移動しません。context.Session ["imageNames"] = lstImageNames; さらにセッションに値を追加しないので、次回のアップロード時にセッションが空になります – ace

+0

@ace私の更新された回答を確認してください – Damith

+0

はい、感謝しました – ace

関連する問題