FileUpload.PostedFile always null inside UpdatePanel
Solution 1
You can put a FileUpload control in an UpdatePanel but you have to use a PostBackTrigger as opposed to an AsyncPostBackTrigger. I recently used this approach and combined both types of trigger.
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
<asp:AsyncPostBackTrigger ControlID="btnAsyncSave"/>
</Triggers>
The PostBackTrigger was used for FileUploads while the AsyncPostBackTrigger was used for all the other form fields.
Solution 2
FileUpload doesn't work inside an UpdatePanel. You must use AsyncFileUpload from ASPNET AJAX control Toolkit.
When you use AsyncFileUpload you must set the right params in the form tag, that is placed in your Page or MasterPage:
<form id="form1" runat="server" enctype="multipart/form-data" method="post">
If you don't set the right enctype and method UploadedComplete will never fire, and you won't be able to get FileUpload.FileBytes since FileUpload.HasFile returns true only during UploadedComplete execution.
Besides, prevoius versions of AsyncFileUpload didn't work on Chrome. Actual version (4.1.50731.0) solved the problem.
Solution 3
This is an old issue going way back to MSAjax 1.0 . The FileUpload control is not compatible with the way Update Panels works and the Script Manager. A file upload modifies the data stream causing the script manager to choke. The only way it will work is to have the File Upload Control outside the update panel. If you have other controls on the page that depends on the file upload control you need to set the add a PostBack trigger.
Solution 4
<Triggers>
<asp:PostBackTrigger ControlID="btnSave" />
</Triggers>
have the File Upload Control outside the update panel. If you have other controls on the page that depends on the file upload control you need to set the add a PostBack trigger.
Steven
Updated on April 08, 2020Comments
-
Steven about 4 years
I have a FileUpload control inside an UpdatePanel. I have successfully registered the upload button with the scriptmanager to do a full postback. The only issue I have is that FileUpload.HasFile is always returning null. Note: When I move the FileUpload control outside of the updatepanel everything works file. Is there a solution or workaround for this.
Thanks.
-
SMI over 10 yearsMy button is saving filename and some other values to database. its id is 'btnSave', then what ControlID should be in PostBackTrigger and what in AsyncPostBackTrigger. Your code seems like you have 2 buttons?
-
rf_wilson over 10 yearsMy code is for a specific example of a FileUpload control inside of an UpdatePanel - this only works with PostBackTrigger, not AsyncPostBackTrigger. I have 2 buttons for other controls that I was using which were not FileUpload and so could work Asyncrounsly.
-
Fandango68 about 5 yearsFor those that just came here, you don't technically need an
AsyncPostBackTrigger
, unless you have controls that require it for the UpdatePanel to recognise them. I found Async more trouble than it's worth.