views:

1558

answers:

6

Hi all,

I'm having some trouble with the FileUpload control and was wondering if I could get some help.

On my page I have a FileUpload control, and a drop down list.

So the user browses to the file they want, and then select an option from the drop down (which enables some checkboxes that are also on the page for use, depending on what they select in the drop down). This causes the FileUpload control to become empty, and now the user must browse to the file they wanted again.

Is there anyway to prevent the FileUpload control from losing its contents during the PostBack?

A: 

Set "EnableViewState" to true for the FileUpload control, that should allow it to keep the value after a post back from another control.

Relster
The file upload control is finicky for security reasons. I could be wrong but I doubt this will work.
Spencer Ruport
Hey Relster,I gave that a try, so I set EnabledViewState=true on the FileUpload control. Yet it still cleared itself after the Postback...In the OnSelectedIndexChanged event handler for the drop down I looked at the FileUpload control, and EnableViewState was indeed set to true.Any other suggestions perhaps?
Denis Sadowski
+3  A: 

So, if I understand correctly, the drop down has the autopostback property set to true and you're checking the value of the drop down to make default changes to the check boxes?

Instead try using client side scripting to do the trick.

Spencer Ruport
Yes the drop down has the AutoPostback property. I thought I put in the actual asp into my post, but I suppose I forgot or it was filtered out of my post.
Denis Sadowski
What happens on drop down change event? Is there a major shift in page functionality?
Robert Koritnik
+2  A: 

Since you tried Relster's suggestion and it didn't work, Spencer is correct. Due to security concerns, it is impossible for anything but the browser to set the path in an <input type="file"/> element. The only solution is to restructure the flow so that the only postback is done when you want to submit the file, and do any other manipulation with client side scripting.

The reason you cannot set the path is because it would allow you to steal users' files. For example, if you hide the input, and have an innocent looking button for postback, you could set the default path of the file input to whatever you wanted to get access to, and the user would upload it without ever knowing what's going on.

Sean Nyman
Thanks, I will certainly be looking at using client side scripting to perform the required changes to the page when the drop down is changed.
Denis Sadowski
Ugh, you agree with me and your answer gets upvoted.Life is so unkind!
Spencer Ruport
+1  A: 

Whenever there's a postback you have to check whether there was also a file coming from the client and save it. You won't be able to re-fill the input box on the client though. I suggest you better hide file upload control (unless you allow multiple file upload) and display filename with check mark beside it. Similar to GMail when you upload files to your email message.

Reasons are security concerns as other pointed out.

I'd also disable any auto postbacks because a drop down change may seem to last an eternity to the client if the upload file size is sufficiently large.

Robert Koritnik
+2  A: 

I will most likely be using client side scripting to fix the issue.

However I've found an solution that makes use of ajax for those people who might be interested.

http://forums.asp.net/t/1125781.aspx

From the post by Jessica Cao, which also contains a code example:

"... you could use AJAX to make asynchronous postback to the server, make the partial page which contains the DropDownList to postback, and the page contains the FileUpload control won't postback, so the FileUpload will maintain the FilePath."

This method requires the use of System.Web.Extensions.dll, and as a result the .Net 3.5 Framework.

Denis Sadowski
A: 

u can't do this -- Simple Solution is to move to Ajax file Control.

Nilang