>  기사  >  웹 프론트엔드  >  AJAX 및 jQuery와 함께 HTML5 파일 업로드 사용

AJAX 및 jQuery와 함께 HTML5 파일 업로드 사용

PHPz
PHPz앞으로
2023-09-13 10:09:03972검색

AJAX 및 jQuery와 함께 HTML5 파일 업로드 사용

양식이 제출되면 제출 프로세스를 캡처하고 다음 코드 조각을 실행하여 파일을 업로드해 보세요. -

// 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);
}

그런 다음 서버 측(예: 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);

위 내용은 AJAX 및 jQuery와 함께 HTML5 파일 업로드 사용의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

성명:
이 기사는 tutorialspoint.com에서 복제됩니다. 침해가 있는 경우 admin@php.cn으로 문의하시기 바랍니다. 삭제