AJAX File Upload in Classic ASP Without a Page Refresh

Keep the page alive while files move, and handle each completion in JavaScript.

What "AJAX upload" buys you

A traditional Classic ASP upload posts the form, the page goes white, and the user waits. Everything they had typed is gone until the server responds, and if the upload fails they get an error page instead of their form.

An AJAX upload sends the file on a separate background request. The page never reloads, so:

  • The rest of the form stays filled in and usable while the file transfers.
  • You can show real progress, because JavaScript is alive during the transfer.
  • A failed upload is a message next to the file, not a lost page.
  • Files are already on the server by the time the user clicks Submit, so the submit is instant.

The mechanics, in three parts

1. The browser sends the file itself

var form = new FormData();
form.append("file", input.files[0]);

var xhr = new XMLHttpRequest();
xhr.open("POST", "upload-handler.asp");

xhr.upload.onprogress = function (e) {
    if (e.lengthComputable) {
        setBar(Math.round(e.loaded / e.total * 100));
    }
};

xhr.onload = function () {
    // the handler's response - typically a file id
    addToQueue(xhr.responseText);
};

xhr.send(form);

2. A handler page receives it

This is an ordinary .asp page whose only job is to read the body, store the file, and return an identifier. It renders no HTML.

3. The main form posts identifiers, not files

When the user finally submits, the form carries a short list of ids. The actual bytes were transferred minutes ago. This is what makes the submit feel instant.

Doing it with ASP Uploader

All three parts are provided. The uploader posts files in the background automatically — the default behaviour, not a mode you switch on — and hands you client-side events to react to.

<%@ Language="VBScript" %>
<!-- #include file="aspuploader/include_aspuploader.asp" -->

<script type="text/javascript">
    function CuteWebUI_AjaxUploader_OnPostback() {
        // called once every queued file has finished uploading
        document.forms[0].submit();
    }
</script>

<form id="form1" method="POST">
    <%
    Dim uploader
    Set uploader = new AspUploader
    uploader.Name = "myuploader"
    uploader.MultipleFilesUpload = true
    uploader.MaxSizeKB = 10240
    uploader.InsertText = "Attach files"
    uploader.AllowedFileExtensions = "*.jpg,*.png,*.gif,*.pdf,*.zip"
    %>
    <%= uploader.GetString() %>
</form>

The client-side events

Function you defineFires when
CuteWebUI_AjaxUploader_OnSelect(files)The user picks files, before any transfer — your chance to reject some
CuteWebUI_AjaxUploader_OnTaskComplete(task)One file finished. task.FileName, task.FileSize available
CuteWebUI_AjaxUploader_OnPostback()The whole queue finished — usually where you submit the form
CuteWebUI_AjaxUploader_OnError(msg)A file was rejected or a transfer failed

A common pattern — build a thumbnail list as files land, without touching the server:

<script type="text/javascript">
function CuteWebUI_AjaxUploader_OnTaskComplete(task) {
    var li = document.createElement("li");
    li.textContent = task.FileName + " (" + Math.round(task.FileSize / 1024) + " KB)";
    document.getElementById("attachments").appendChild(li);
}
</script>

<ul id="attachments"></ul>

Uploading with no form post at all

Sometimes there is no form to submit — a file manager, a gallery, a drop zone that should just save the file immediately. Point the uploader at a destination folder and it writes files as they arrive:

<%
Set uploader = new AspUploader
uploader.Name = "myuploader"
uploader.MultipleFilesUpload = true
uploader.SaveDirectory = "savefiles"       ' written as each file completes
uploader.Render()
%>

There are live examples of both styles: an AJAX multiple-file upload and an attachment list.

Three gotchas

Session state. The background upload request is a normal HTTP request and carries the session cookie — but if your handler checks a session variable that is only set on the parent page, make sure it is set before the first file can be selected.

  • Do not submit the form while uploads are running. Disable the submit button until OnPostback fires, or the user will post a half-finished queue.
  • Clean up orphans. Files uploaded in the background belong to nobody until the form is submitted. If the user closes the tab, you have a temp file to expire.
  • Handle the back button. A user who navigates back to the form should see the files still attached — carry the id list in a hidden field, as in the keeping-state pattern.

Frequently asked questions

Can Classic ASP handle an AJAX file upload?

Yes. The receiving handler is an ordinary .asp page; what makes it "AJAX" is that the browser sends the file with XMLHttpRequest instead of a form post.

Does an AJAX upload still need multipart/form-data?

If you send a FormData object, yes — the browser sets that encoding for you. Chunked uploaders often post raw bytes instead, which is simpler for the server to read.

Will it work if JavaScript is disabled?

No, and no AJAX upload can. ASP Uploader degrades to a standard file input in that case.

Skip the plumbing

ASP Uploader does everything on this page out of the box: multi-file selection, a real progress bar, client and server validation, chunked large-file transfer, and no COM component to register. Drop the folder on your server and add one include line.

Download the free trial Try the live demo Pricing