Skip to main content

How to use standard 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" on 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 the control which lies on UpdatePanel, FileUpload is always empty when it has 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 at 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

  1. Yess.. this is what i'm looking for..
    only need 1 row
    this.Page.Form.Enctype = "multipart/form-data";

    thank you so much!!

    ReplyDelete

Post a Comment

Popular posts from this blog

How to merge cells with equal values in the GridView

My solution is not the first; however, I think, it is rather universal and very short - less than 20 lines of the code.

The algorithm is simple: to bypass all the rows, starting from the second at the bottom, to the top. If a cell value is the same as a value in the previous (lower) row, then increase RowSpan and make the lower cell invisible, and so forth.

The code that merges the cells is very short:
public class GridDecorator { public static void MergeRows(GridView gridView) { for (int rowIndex = gridView.Rows.Count - 2; rowIndex >= 0; rowIndex--) { GridViewRow row = gridView.Rows[rowIndex]; GridViewRow previousRow = gridView.Rows[rowIndex + 1]; for (int i = 0; i < row.Cells.Count; i++) { if (row.Cells[i].Text == previousRow.Cells[i].Text) { row.Cells[i].RowSpan = previousRow.Cells[i].RowSpan < 2 ? 2 : prev…

Merging columns in GridView/DataGrid header

As necessity to show header columns in a few rows occurs fairly often it would be good to have such functionality in the GridView/DataGrid control as an in-built feature. But meanwhile everyone solves this problem in his own way.

The described below variant of the merging implementation is based on irwansyah's idea to use the SetRenderMethodDelegate method for custom rendering of grid columns header. I guess this approach can be simplified in order to get more compact and handy code for reuse.
The code overview
As it may be required to merge a few groups of columns - for example, 1,2 and 4,5,6 - we need a class to store common information about all united columns.
[Serializable]
private class MergedColumnsInfo
{
// indexes of merged columns
public List<int> MergedColumns = new List<int>();
// key-value pairs: key = the first column index, value = number of the merged columns
public Hashtable StartColumns = new Hashtable();
// key-value pairs: key = the first column in…

JIRA REST API: Cookie-based Authentication

Three authentication methods are proposed by the JIRA REST API documentation: Basic Authentication is a simple but not very safe approach. Credentials are sent in the header on every request and encoding to Base64 is not a proper protection in this case; HTTPS connection is required. OAuth authentication - looks a bit complex and requires additional configuration at the JIRA server that is not always possible. Cookie-based Authentication - this approach seems to be the most convinient one: credentials are checked once, then the authentication cookie only is sent on subsequent requests. However, trying to use the cookie-based authentication I encountered an issue. The approach described in the documentation worked partially: I was able to create a new session and get the response containing the session cookie but all subsequent requests using this session cookie were rejected as unauthorized. Spending some time investigating this I found the cause of the issue: JSESSIONID is not th…