Prachi
Prachi

Reputation:

File uploading on different pages

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.

Upvotes: 1

Views: 1788

Answers (8)

Kirtan
Kirtan

Reputation: 21695

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.

Upvotes: 1

NileshChauhan
NileshChauhan

Reputation: 5569

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.

Upvotes: 1

rahul
rahul

Reputation: 187030

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

Upvotes: 0

DevMania
DevMania

Reputation: 2341

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.

Upvotes: 0

Chad Grant
Chad Grant

Reputation: 45382

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

Upvotes: 0

Guffa
Guffa

Reputation: 700292

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.

Upvotes: 0

Cerebrus
Cerebrus

Reputation: 25775

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.

Upvotes: 2

Kirtan
Kirtan

Reputation: 21695

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

Upvotes: 0

Related Questions