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

D

David Thielen

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?
 
S

Steven Cheng[MSFT]

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.
 
D

David Thielen

Hello;

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

Steven Cheng[MSFT]

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.
 
D

David Thielen

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?
 
S

Steven Cheng[MSFT]

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.
 
D

David Thielen

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.
 
S

Steven Cheng[MSFT]

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,734
Messages
2,569,441
Members
44,832
Latest member
GlennSmall

Latest Threads

Top