Implementation of AJAX File Upload Using HTML5 and jQuery

Nov 28, 2025 · Programming · 10 views · 7.8

Keywords: HTML5 | File_Upload | AJAX | jQuery | FileReader

Abstract: This paper provides an in-depth exploration of implementing complete form file upload functionality by combining HTML5 File API with jQuery AJAX. Through analysis of the core mechanisms of the FileReader interface, it elaborates on the complete process including client-side file reading, asynchronous transmission, and server-side file processing. The article adopts a hybrid approach using native JavaScript and jQuery, ensuring compatibility with modern browsers while leveraging jQuery's convenience. Alternative pure JavaScript implementation solutions are also compared, providing developers with multiple technical options.

Technical Background and Requirements Analysis

In modern web application development, file upload functionality has become a fundamental requirement. Traditional form submissions cause page refreshes, negatively impacting user experience. The emergence of HTML5 File API provides native support for asynchronous file uploads, which can be combined with AJAX technology to achieve refresh-free uploading. This paper addresses practical development needs by exploring how to implement complete form asynchronous submission including file fields.

Core Implementation Principles

The HTML5 File API provides the FileReader interface, allowing web applications to asynchronously read file contents stored on users' computers. Through methods like readAsText and readAsDataURL, file contents can be converted into different formats for transmission.

Client-Side Implementation Solutions

File Selection and Reading

First, obtain the file input element and read the file content:

var file = document.getElementById('fileBox').files[0];
var reader = new FileReader();
reader.readAsText(file, 'UTF-8');
reader.onload = shipOff;

Asynchronous Transmission Processing

After file reading completes, use jQuery's $.post method for asynchronous transmission:

function shipOff(event) {
    var result = event.target.result;
    var fileName = document.getElementById('fileBox').files[0].name;
    $.post('/myscript.php', { data: result, name: fileName }, continueSubmission);
}

Event Handling Mechanism

The FileReader provides a complete event handling mechanism:

Server-Side Processing

On the PHP server side, receive and save the uploaded file:

$data = $_POST['data'];
$fileName = $_POST['name'];
$serverFile = time().$fileName;
$fp = fopen('/uploads/'.$serverFile,'w');
fwrite($fp, $data);
fclose($fp);
$returnData = array( "serverFile" => $serverFile );
echo json_encode($returnData);

Alternative Implementation Solutions

Pure JavaScript Implementation

Using the FormData object can simplify the file upload process:

async function uploadFile() {
    let formData = new FormData();
    formData.append("file", fileupload.files[0]);
    await fetch('/upload.php', {
        method: "POST",
        body: formData
    });
    alert('File uploaded successfully');
}

Simplified Server-Side Processing

Corresponding PHP processing code:

$filename = $_FILES['file']['name'];
$location = "upload/".$filename;
if ( move_uploaded_file($_FILES['file']['tmp_name'], $location) ) {
    echo 'Success';
} else {
    echo 'Failure';
}

Technical Comparison Analysis

FileReader Solution Advantages

FormData Solution Advantages

Practical Application Recommendations

When selecting implementation solutions, consider the following factors:

Conclusion

The HTML5 File API provides powerful native support for web file uploads. By appropriately choosing between FileReader or FormData solutions, developers can efficiently implement asynchronous file upload functionality. The two implementation solutions provided in this paper each have their advantages, allowing developers to select the most suitable technical path based on specific requirements.

Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.