views:

484

answers:

9

Hi,

I am stuck in uploading files at different pages.On one page i open a popup where i ask user to browse and select files and on close option,the popup gets closed and then i want to upload files from the main page, not from the popup. How can i do that? I am using the following code to obtain uploaded files:

HttpFileCollection uploads = HttpContext.Current.Request.Files;

How can I access this value from the main page, I am using HttpFilecollection on the popup window.

A: 

You can try using cross-page postbacks which is a new feature in ASP.NET 2.0

Kirtan
+2  A: 

If you give some thought to it, your question is not actually how to upload files from a different page. It is "How do I pass values from one page to another in ASP.NET?"

Good news is that there are a million articles on the web explaining how to do this:

a. Cross page postbacks

b. How to pass values between ASP.NET pages (MSDN)

c. Another article by Steve C. Orr on Passing values.

The value you need to pass is the HttpFileCollection from the popup page to the parent page. Then you can iterate through each HttpPostedFile in the collection and call Save on it as per your logic.

Cerebrus
A: 

That is not possible.

The upload has to be done by the page where the upload control is. You can not transfer the information to another window and let it do the upload instead.

When the upload is sent to the server, you have to take care of it right away. The files that are uploaded only exist in the file collection of that request, if you don't take care of the file data when handling that request, it's gone.

So, if you have the upload control in the popup, the popup has to do the upload. You have to take care of the uploaded files when the popup page is posted, but you can of course put some information about the uploaded files in the response, that the popup can send to the main page when the popup closes itself.

Guffa
Are you sure about this or does your answer need to be additionally qualified? I have been able to pass a HttpFileCollection from one page to another in the past. I haven't known you to be mistaken... yet! ;-)
Cerebrus
Well, it may very well be possible to pass the HttpFileCollection, and I would consider that as a form of "taking care of the file data". Although I would recommend getting the file data from the collection and pass, rather than relying on that the collection is not disposed along with it's Request object.
Guffa
A: 

The issue is that you cannot set the value of an input type="file" element for security reasons. You don't want evil websites trying to upload your C:\whateverfiletheywant.dat

So you have to do the upload in the popup into a temp upload directory and send the filenames the user uploaded to the master form via javascript (window.opener)

I have done this many times.

Something similiar to what I do if I have to upload something in a popup.

List<string> filesUploaded = new List<string>();
foreach (HttpPostedFile file in HttpContext.Current.Request.Files)
{
    if (file.ContentLength <= 0)
        continue;

    string filename = String.Format("{0}.jpg",Regex.Replace(Guid.NewGuid().ToString(), "[^A-Za-z0-9]*", String.Empty));
    file.SaveAs(Path.Combine(Server.MapPath("/upload/temp/"), filename));
    filesUploaded.Add(filename);
}
Response.Write(String.Format("<{0}>window.opener.FilesUploaded([{1}]);</{0}>","script", String.Join(",",filesUploaded.ToArray()).TrimEnd(new char[]{','})));

*note ... asp doesnt like script tags in your c# code, so thats why the script keyword is in the String.Format

Chad Grant
A: 

hi, from my experience i really like telerik upload control, it offers many things like Validation, file size, allowed extensions, etc.. , but if you want real time progress, you are better off with silver light or flash based upload control, telerik offers that.

here are some links on achieving it with flash + ASP.Net

hope this helps.

i mean i only want user to select fiels on one page and close the pop up and upload fiels from main page which shows fiels selected by user in attachments feild like it is done in MS outlook.
i think with telerik you can achieve that, check their demos on the site really nice, and you can ask them this question they answer pretty fast.
A: 

HI, You can pass the file names to the parent window using

opener.SomeFunction

and pass filenames as an array as the argument to the function.

Hope this helps

rahul
thanks..but i want to show files in the form MS outlook does.And what format is that and how to show files in that form?
Do you want to display the filename with extension and file size??
rahul
+1  A: 

I answered this for you a couple of days ago http://stackoverflow.com/questions/792252/file-uploading-on-different-pages

Chad Grant
+1  A: 

In JavaScript, window has a property called "opener", which refers to the parent windows.

So you can call window.opener to access the parent window from the pop-up and do whatever u want to achieve.

nils_gate
+1  A: 

For doing it using JavaScript, you can use the document.createElement method.

For doing it using ASP.NET, use the HtmlTableRow class, and add rows dynamically to your table.

I recommend the JavaScript approach.

Kirtan