Skip to main content

FileUpload in AJAX-enabled web applications


I would like to note that this article is not about the ability to upload files to the server without the postback. There are a lot of articles on this topic, just type "AJAX FileUpload" in any search engine and you'll get many examples. However with AJAX they actually have little in common, because the XMLHttpRequest does not support asynchronous uploading of files to the server, they are rather a variety of imitations, for example, using hidden IFRAME element. Nevertheless I want to emphasize that the article is not about that but about the standard FileUpload control.
There are two problems you might encounter when using it on UpdatePanel.
Problem 1
If the postback is caused by a control which lies on the UpdatePanel, the FileUpload is always empty when it come to the server, regardless whether a file has been selected or not.
Example:
<asp:UpdatePanel ID="UpdatePanel1" runat=server>
  <ContentTemplate>
     <asp:FileUpload ID="FileUpload1" runat=server />
     <asp:Button ID="btnUpload" runat=server Text="Upload" OnClick="btnUpload_Click"/>
  </ContentTemplate>
</asp:UpdatePanel>
Solution
As XMLHttpRequest does not allow to send files asynchronously, they have to be submitted in a common manner. This problem is well described around, it is solved by registration of the control that has to submit the form as a postback trigger (in the above example it is btnUpload button).
<asp:UpdatePanel ID="UpdatePanel1" runat=server>
  <ContentTemplate>
     <asp:FileUpload ID="FileUpload1" runat=server />
     <asp:Button ID="btnUpload" runat=server Text="Upload 2" OnClick="btnUpload_Click"/>
  </ContentTemplate>
  <Triggers>
      <asp:PostBackTrigger ControlID="btnUpload" />
  </Triggers>
</asp:UpdatePanel>

Problem 2
FileUpload does not work if it is loaded not on the initial page load but appears only after asynchronous update of the page part.
Example (pnlUpload panel is invisible in the beginning and is shown after clicking on btnShowFileUpload button):
<asp:UpdatePanel ID="UpdatePanel1" runat=server>
   <ContentTemplate>
        <asp:Button ID="btnShowFileUpload" runat=server Text="Show File Upload" OnClick="btnShowFileUpload_Click"/>
        <asp:Panel ID="pnlUpload" runat=server Visible="False">
           <asp:FileUpload ID="FileUpload1" runat=server />
           <asp:Button ID="btnUpload" runat=server Text="Upload" OnClick="btnUpload_Click"/>
       </asp:Panel>
   </ContentTemplate>
   <Triggers>
        <asp:PostBackTrigger ControlID="btnUpload" />
   </Triggers>
</asp:UpdatePanel>

.......................

protected void btnShowFileUpload_Click(object sender, EventArgs e)
{
    pnlUpload.Visible = true;
}
Solution
The problem is caused by the requirement that for the normal work of FileUpload the form should have enctype="multipart/form-data". Usually, it is set in overriden OnPreRender method of FileUpload control.
protected internal override void OnPreRender(EventArgs e)
{
    base.OnPreRender(e);
    HtmlForm form = this.Page.Form;
    if ((form != null) && (form.Enctype.Length == 0))
    {
        form.Enctype = "multipart/form-data";
    }
}
Although during asynchronous postback this code is also executed but the form is not updated. That is why it is required to set the form content type explicitly during the first page load, for example, in the Page_Load event handler of the page or a control where FileUpload is placed.
protected void Page_Load(object sender, EventArgs e)
{
    if (!IsPostBack)
        this.Page.Form.Enctype = "multipart/form-data";
}
In case if this task is repeated in a few places you may do a simple control derived from FileUpload with overriden OnLoad method and use it.
public class CustomFileUpload : FileUpload
{
 protected override void OnLoad(EventArgs e)
 {
     base.OnLoad(e);

     if (!Page.IsPostBack)
         this.Page.Form.Enctype = "multipart/form-data";
 }
}

Comments

Popular posts from this blog

Editing Child GridView in Nested GridView

Editing Child GridView in Nested GridView In this article we will explore how to edit child gridview in the nested gridview.   Let''s write some code. Step 1:  Add scriptmanager in the aspx page. < asp : ScriptManager   ID ="ScriptManager1"   runat ="server"   EnablePageMethods ="true"> </ asp : ScriptManager > Step 2:  Add below stylesheet for modal popup. < style   type ="text/css">        .modalBackground        {              background-color : Gray;              filter : alpha(opacity=80);              opacity : 0.5;       }        .ModalWindow        {              border : solid1px#c0c0c0;              background : #f0f0f0;              padding : 0px10px10px10px;              position : absolute;              top : -1000px;       } </ style > Step 3:   Create an aspx page and add a Gridview with another gridview in the last TemplateField. The last templatefield will also contain a lable which will

Hierarchical GridView in ASP.NET with AJAX, JQuery implementation - Part 1

Previously I had blogged some post related to showing two grids in hierarchical way. For example - when I need to show an invoice reports on the screen, the user requesting to show the list of invoices at initial time and they can drill down the details of item details of each invoice by clicking on respective row. The implementations can be in multiple ways, but I had blogged the implementation with AJAX and without AJAX using Java script on ASP.NET page. Below are the URLs of the same – Hierarchical GridView in ASP.NET Hierarchical GridView in ASP.NET with AJAX, Javascript implementation In this post I am taking the same requirements done before and enhance with some additional functionality as per readers expectations. The requirement on this implementation will be – The page should show a Grid View with list of Orders with Expand icon on the first column. On click of expand icon on each row, the Order Details of the Order must be fetched from the database

Scrollable Gridview With fixheader using JQuery in Asp.net

Scrollable Gridview With fixheader using JQuery in Asp.net Introduction: In this article I will explain how to implement scrollable gridview with fixed header in asp.net using JQuery.  Description:  In Previous posts I explained lot of articles regarding Gridview. Now I will explain how to implement scrollable gridview with fixed header in asp.net. I have one gridview that contains lot of records and I used  paging for gridview  but the requirement is to display all the records without paging. I removed paging at that time gridview occupied lot of space because it contains more records to solve this problem we implemented scrollbar.  After scrollbar implementation if we scroll the gridview we are unable to see Gridview header.   To implement Scrollable gridview with fixed header I tried to implement concept with css and JavaScript but there is no luck because maintaining fixed header working in IE but not in Mozilla and vice versa to solve this browser compatibility proble