Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FileUpload.PostedFile always null inside UpdatePanel

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.

like image 820
Steven Avatar asked Jun 07 '11 14:06

Steven


2 Answers

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.

like image 168
rf_wilson Avatar answered Oct 17 '22 23:10

rf_wilson


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.

like image 27
Emanuele Greco Avatar answered Oct 17 '22 23:10

Emanuele Greco