pop-up browser persist data back to parent page code-behind

Discussion in 'ASP .Net Web Controls' started by David Thielen, Mar 23, 2006.

  1. Hi;

    Here is what I want to do. On my main page there will be a button that says
    "Get File" pressing that pops up a second browser window with a FileUpload
    control in it. The user selects a file and then presses submit in this popup
    window.

    The file is uploaded, the pop-up browser goes away, the file is saved in the
    class for the main window code behind, and the main page is redrawn to show
    that the file has been uploaded.

    Can I do this and if so - how?

    --
    thanks - dave
    david_at_windward_dot_net
    http://www.windwardreports.com
     
    David Thielen, Mar 23, 2006
    #1
    1. Advertising

  2. Hi Dave,

    When we open a new browser window from one browser window/page, the
    relation between them can be get through client-side script, server-side
    page model have no sense of client windows. And for opener and child
    window, we can use the "window.opener" script code to reference them.

    So, for your scenario, I think you can consider registering a client-script
    block after the upload page(sub page) have processed and saved the uploaded
    file. The script will call a script function in the opener page and close
    itself. e.g;

    ================
    public partial class Upload_UploadDlg : System.Web.UI.Page
    {
    public const string SCRIPT = @"
    <script language='javascript'>
    window.opener.end_upload();

    window.self.close();
    </script>
    ";

    protected void Page_Load(object sender, EventArgs e)
    {

    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
    if (fu1.HasFile)
    {
    Session["upload_file_name"] = fu1.FileName;

    lblMessage.Text = "file is uploaded at " +
    DateTime.Now.ToLongTimeString();

    Page.ClientScript.RegisterStartupScript(this.GetType(),
    "close_script", SCRIPT);
    }
    }
    }
    ===========================


    as you see, in the upload page, i stored the uploaded file's name in the
    session for later use. Then, register the client script to call a script
    function defined in opener page and close itself.

    In the opener page, the clientscript is like below:

    ============
    function end_upload()
    {
    document.getElementById("btnSubmit").click();
    }
    =============

    it programmatically call a button's click method to submit the page. We can
    then do some processing on the server and read the uploaded filename from
    session.

    BTW, if you do not need to do any server-side processing on the opener
    page, just want to show the uploaded file's name. We can just let the
    upload page's script pass the name to opener page through the script
    function, that can avoid additional postback.

    Hope this helps.


    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may

    learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 23, 2006
    #2
    1. Advertising

  3. Hello;

    I am missing something here. I don't see how the file contents and filename
    are getting back to the main page???

    --
    thanks - dave
    david_at_windward_dot_net
    http://www.windwardreports.com



    "Steven Cheng[MSFT]" wrote:

    > Hi Dave,
    >
    > When we open a new browser window from one browser window/page, the
    > relation between them can be get through client-side script, server-side
    > page model have no sense of client windows. And for opener and child
    > window, we can use the "window.opener" script code to reference them.
    >
    > So, for your scenario, I think you can consider registering a client-script
    > block after the upload page(sub page) have processed and saved the uploaded
    > file. The script will call a script function in the opener page and close
    > itself. e.g;
    >
    > ================
    > public partial class Upload_UploadDlg : System.Web.UI.Page
    > {
    > public const string SCRIPT = @"
    > <script language='javascript'>
    > window.opener.end_upload();
    >
    > window.self.close();
    > </script>
    > ";
    >
    > protected void Page_Load(object sender, EventArgs e)
    > {
    >
    > }
    > protected void btnUpload_Click(object sender, EventArgs e)
    > {
    > if (fu1.HasFile)
    > {
    > Session["upload_file_name"] = fu1.FileName;
    >
    > lblMessage.Text = "file is uploaded at " +
    > DateTime.Now.ToLongTimeString();
    >
    > Page.ClientScript.RegisterStartupScript(this.GetType(),
    > "close_script", SCRIPT);
    > }
    > }
    > }
    > ===========================
    >
    >
    > as you see, in the upload page, i stored the uploaded file's name in the
    > session for later use. Then, register the client script to call a script
    > function defined in opener page and close itself.
    >
    > In the opener page, the clientscript is like below:
    >
    > ============
    > function end_upload()
    > {
    > document.getElementById("btnSubmit").click();
    > }
    > =============
    >
    > it programmatically call a button's click method to submit the page. We can
    > then do some processing on the server and read the uploaded filename from
    > session.
    >
    > BTW, if you do not need to do any server-side processing on the opener
    > page, just want to show the uploaded file's name. We can just let the
    > upload page's script pass the name to opener page through the script
    > function, that can avoid additional postback.
    >
    > Hope this helps.
    >
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may
    >
    > learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
     
    David Thielen, Mar 23, 2006
    #3
  4. Hi Dave,

    I just store the uploaded file¡¯s file name in a Session variable, thus
    when the main page postback, the code can check that session variable to
    get the filename. Also, if you want to access any other info about the
    file, you can provide a path to the temp file on the server so that it can
    access that file¡¯s content.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may

    learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 24, 2006
    #4
  5. Hi;

    Oh, I get it now. Temp file on the server. Any suggestions on how to do this
    if I don't want a temp file on the server?

    --
    thanks - dave
    david_at_windward_dot_net
    http://www.windwardreports.com



    "Steven Cheng[MSFT]" wrote:

    > Hi Dave,
    >
    > I just store the uploaded file¡¯s file name in a Session variable, thus
    > when the main page postback, the code can check that session variable to
    > get the filename. Also, if you want to access any other info about the
    > file, you can provide a path to the temp file on the server so that it can
    > access that file¡¯s content.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may
    >
    > learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
    >
     
    David Thielen, Mar 24, 2006
    #5
  6. Thanks for your reply Dave,

    In addition to using temp file, you can also consider storeing the file
    content in memory or session, however, both of these two will cause serious
    consumsion of server machine's memory which is not recommended. Another
    option is use temp database to store the file content(binary data). Anyway,
    the file content must be cached on server-side so that it can be reused
    between multiple postbacks. And we need to delete as soon as we've finished
    using it so as not to produce redundant cost.

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 27, 2006
    #6
  7. Hi;

    How can I attach the file contents to the session so my original page then
    has it to access from it's code behind? This will be a rare event so we
    should usually have zero instances of this and occasionally 1 file content
    attached to a session so I'm not worried about memory use.

    --
    thanks - dave
    david_at_windward_dot_net
    http://www.windwardreports.com



    "Steven Cheng[MSFT]" wrote:

    > Thanks for your reply Dave,
    >
    > In addition to using temp file, you can also consider storeing the file
    > content in memory or session, however, both of these two will cause serious
    > consumsion of server machine's memory which is not recommended. Another
    > option is use temp database to store the file content(binary data). Anyway,
    > the file content must be cached on server-side so that it can be reused
    > between multiple postbacks. And we need to delete as soon as we've finished
    > using it so as not to produce redundant cost.
    >
    > Regards,
    >
    > Steven Cheng
    > Microsoft Online Community Support
    >
    >
    > ==================================================
    >
    > When responding to posts, please "Reply to Group" via your newsreader so
    > that others may learn and benefit from your issue.
    >
    > ==================================================
    >
    >
    > This posting is provided "AS IS" with no warranties, and confers no rights.
    >
    >
     
    David Thielen, Mar 28, 2006
    #7
  8. Thanks for the reply Dave,

    As for the file content, if they're binary file, you can just read out the
    binary content out of the stream and store into a byte[] array, then, we
    can directly put the byte array into Session through

    byte[] myfilebytes = xxxx;
    ......
    Session["file_bytes"] = myfilebytes;

    Regards,

    Steven Cheng
    Microsoft Online Community Support


    ==================================================

    When responding to posts, please "Reply to Group" via your newsreader so
    that others may learn and benefit from your issue.

    ==================================================


    This posting is provided "AS IS" with no warranties, and confers no rights.
     
    Steven Cheng[MSFT], Mar 29, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bruce barker
    Replies:
    1
    Views:
    2,805
    =?Utf-8?B?Vmk=?=
    Aug 12, 2004
  2. Steve C. Orr [MVP, MCSD]

    Re: Opening a pop-up window from code behind page

    Steve C. Orr [MVP, MCSD], Aug 12, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    455
    Steve C. Orr [MVP, MCSD]
    Aug 12, 2004
  3. Samy
    Replies:
    0
    Views:
    544
  4. ram_kri
    Replies:
    4
    Views:
    10,371
    ram_kri
    Sep 1, 2006
  5. Erich93063
    Replies:
    2
    Views:
    337
    Erich93063
    Jan 3, 2007
Loading...

Share This Page