Recently I ran into an issue, that by the looks of Google, is affecting many others too.Ã‚Â I wanted to be able to allow users to select an upload path, using the standard Asp.Net fileUpload control, and maintain that control’s path through postbacks, so I could perform some server side validation.Ã‚Â As far as I can tell, this is impossible.Ã‚Â It is also only possible to get the control’s path, programatically when a full postback occurs and for security reasons, the control does not allow its path to be programatically set, so storing the path prior to the postback and restoring it, is not an option either.Ã‚Â Ã‚Â So what this means is there is no way to accomplish what I needed to do using the standard fileUpload control.
Recently the AjaxControlToolkit has introduced a new control, the AsyncFileUpload control. http://www.asp.net/ajax/ajaxcontroltoolkit/Samples/AsyncFileUpload/AsyncFileUpload.aspx
This control can be set up to look identical to the standard fileUpload control, but immediately performs a file upload once the user selects their file path. This allows async file uploading! Problem solved! ….. well…..sorta
I also wanted a way to disable all other asyncFileUpload controls on a page while an upload is in progress, this was preventing an “Unknown Server Error” browser alert message when trying to perform 2 async uploads at once. I google’d and google’d for this problem, and could not find anyone who was having a similar issue, or a fix for it, so I had to go for a work around.
In a nutshell, I implemented the client-side uploadStarted, and uploadCompleted events on the AsyncFileUpload control, and used some Jquery within those to disable specific controls using css classes. Problem with this was that once an AsyncFileUpload control is disabled, its file path can no longer be read server-side until the page is refreshed again.Ã‚Â I am not sure if this is a bug, or is by design, but never the less, it was not working properly, and time was not on my side.
So I had to develop another workaround…
I decided I could show the user a button and textbox that was rigged to look JUST like a file upload control. I could populate the textbox with the fileUpload control’s path, and they would never know the difference….hopefully.
After a long battle with the fileUpload controls, I managed to get a fairly suitable workaround…. when a file starts uploading, the “fake” file upload control appears, and when the file is done uploading, the real one comes back.
Let’s start with the code:
In the code behind you can register the script as a client script block, in the Page_Load or somewhere similar.Ã‚Â Wherever you prefer, just so that it is included on the rendered page. The code I used, is below.Ã‚Â It uses Jquery to find elements based on their css class, and to hide all AsyncFileUploads on the page once an upload startsÃƒÂ¢Ã¢â€šÂ¬Ã‚Â¦ and then bring those back when the upload is completeÃƒÂ¢Ã¢â€šÂ¬Ã‚Â¦ along with populating our ÃƒÂ¢Ã¢â€šÂ¬Ã…”fakeÃƒÂ¢Ã¢â€šÂ¬Ã‚Â fileupload with the file upload path that is used, to make it all look like its a real file upload control that is just disabled.Ã‚Â Confusing? It’s not too bad, it’s just hard to explain:
* fake file upload control (button and textbox), that is displayed when
* a file is being uploaded
* This lets a user only upload one file at a time, and
* prevents errors from the AsyncFileUpload control
function UploadStarted(sender, args)
function UploadComplete(sender, args)
Here is what the ASPX looks like
<asp:Panel ID="pnlFile" runat="server" Visible="false" Width="325">
<div class="fakeAsyncFileUpload" style="display: none;">
<asp:TextBox ID="fakeTxt" CssClass="fakeAsyncFileUploadText" Width="207"
<asp:Button ID="fakeBtn" runat="server" Enabled="false" Text="Browse" />
<asp:Image ID="throbberImg" runat="server" ImageUrl="~/images/indicator.gif"
left: 2px; top: 4px;" />
<ajaxtoolkit:asyncfileupload id="btnFileUpload" cssclass="asyncFileUpload"
persistfile="true" runat="server" width="280" errorbackcolor="White"
As you can see, each control has its own style for a purpose. That way they can easily be hidden/shown using Jquery
and client-side events of the AsyncFileUpload control.
This was my workaround to the whole ÃƒÂ¢Ã¢â€šÂ¬Ã…”fileUpload control clearing on postbackÃƒÂ¢Ã¢â€šÂ¬Ã‚Â issue. Hopefully it can benefit others who were having the same problem I was.