Upload files in an asynchronous way
This is an old project of mine, that I created back in 2007.
Imported into GitHub for archival puposes only.
There will be no updates.
I was working on a project and I needed to upload a series of file to the web server (one at a time), without refreshing the whole page. While the ASP.NET AJAX library is quite useful with the UpdatePanel
that permits a wide range of page updates without refreshing the whole thing, it's currently incompatible with file uploading.
Searching through the web, I found a few techniques to upload a file to a web server: using a proprietary component, using the ADODB.Stream, and using a hidden IFrame.
The proprietary component was out of the question, not just because the probable price tag, but also because the requirement to install something in the user's box.
Using the ADODB.Stream, while a valid solution, it not only prevents using it in a cross-browser manner, as well, requires a change of configuration of the Internet Explorer on the user's box.
So, the technique used by this control is the hidden IFrame, which allow a cross browser functionality, while creating the desired result.
When the user selects a file to upload the control starts a post back directing the result to a hidden IFrame and when the file is received by the server the control raises at the server the UploadedFile
event.
During the UploadedFile
event the page housing the control can access the uploaded file by accessing the property of the event arguments. In C#
add the handler in the constructor method. VB
users can add the event handler in the Sub New
or adding the Handles...
directive at the appropriated method.
None of the page events or initialization methods and events (OnInit
, Init
, OnInitComplete
, InitComplete
, OnLoad
, Load
, OnLoadComplete
and LoadComplete
) are called when an uploaded file is received by the server. The reasons for this behavior are unknown, but welcome, at this point.
Each file receives a GUID that is passed in the event arguments of the UploadedFile event, and refereed to in the RemovedFile event, and in the FileList property.
If the user removes a file from the list of uploaded files, the event RemovedFile
is raised.
Update: I have changed the sample page in order to save the uploaded file under the project's root, in order to use the VirtualPath property.
Known Issues:
- Currently the AjaxUpload control is incompatible with the .NET Framework 3.5 UpdatePanel. It will throw an exception in the
Sys$WebForms$PageRequestManager$_endPostBack
function.
To work around this issue, add aPostBackTrigger
to theUpdatePanel
specifying the AjaxUpload control as its source.