Using HTML5 file upload with AJAX and jQuery

PHPz
Release: 2023-09-13 10:09:03
forward
1039 people have browsed it

Using HTML5 file upload with AJAX and jQuery

When the form is submitted, capture the submission process and try to run the following code snippet to upload the file -

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

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

Then, on the server side (i.e. myscript.php ) -

$data = $_POST['data'];
$fileName = $_POST['name'];
$myServerFile = time().$fileName;

// Prevent overwriting
$fp = fopen('/uploads/'.$myServerFile,'w');
fwrite($fp, $data);
fclose($fp);
$retData = array( "myServerFile" => $myServerFile );
echo json_encode($retData);
Copy after login

The above is the detailed content of Using HTML5 file upload with AJAX and jQuery. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template