0

ページ1にリストボックスとボタンがあり、ボタンをクリックすると、ページ2が新しいタブで開きます。ページ 2 では、写真をフォルダーにアップロードし、session["FileName"] 値を設定しています。2 ページを閉じると、アップロードした画像の名前がリストボックスに表示されます。

注: session["FileName"] = アップロードされた画像の名前。

誰にもアイデアはありますか?私を助けてください。

ありがとうございました。

私のアップロードクラス:

public void ProcessRequest(HttpContext context)
{      
    if (context.Request.Files.Count > 0)
    {
        // get the applications path 

        string uploadPath = context.Server.MapPath(context.Request.ApplicationPath + "/Temp");
        for (int j = 0; j <= context.Request.Files.Count - 1; j++)
        {
            // loop through all the uploaded files 
            // get the current file 
            HttpPostedFile uploadFile = context.Request.Files[j];

            // if there was a file uploded 
            if (uploadFile.ContentLength > 0)
            {
                context.Session["FileName"] = context.Session["FileName"].ToString() + uploadFile.FileName+",";
                uploadFile.SaveAs(Path.Combine(uploadPath, uploadFile.FileName));
            }
        }
    }
    // Used as a fix for a bug in mac flash player that makes the 
    // onComplete event not fire 
    HttpContext.Current.Response.Write(" ");
}
4

1 に答える 1

0

セッションは、ASP.NET のサーバー オブジェクトです。つまり、あるページで Session を作成すると、Session オブジェクトが削除されていないか期限切れになっていない限り、他のページでそれを使用できます。

これを page1.aspx.cs で行ったとします。

Session["FileName"] = "file1";

次に、次のように page2.aspx.cs でアクセスできます。

if(Session["FileName"]!=null)
    Label1.Text = (string)Session["FileName"]

そのため、.aspx ページまたは Control 派生クラスでのみセッション変数にアクセスできます。

クラス ライブラリ プロジェクト内のセッション変数にアクセスする場合は、次のようにします。

HttpContext.Current.Session["FileName"]

また、 Custom を作成したようですHttpModule

セッション状態が初期化される前に発生するパイプライン イベントを HTTPModule で処理してはならないことに注意してください。

HttpModule でセッション変数にアクセスする方法とタイミングについて詳しく知るには、これをお読みください。

于 2013-04-20T09:43:07.150 に答える